RestTemplate带参发送get及post请求携带headers请求头代码示例
生活随笔
收集整理的這篇文章主要介紹了
RestTemplate带参发送get及post请求携带headers请求头代码示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、發送get請求攜帶headers
@Testpublic void testGetWithHeaders(){HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);Map<String,Integer> map = new HashMap<String,Integer>();map.put("pageNum",1);map.put("pageSize",15);HttpEntity<MultiValueMap> httpEntity = new HttpEntity<>(null, headers);//get請求String newUrl = "https://xxxxxxx.com:8101/operate/operate_pay/getOperatesPay?pageNum="+1+"&pageSize="+15;ResponseEntity<String> responseEntity = restTemplate.exchange(newUrl, HttpMethod.GET, httpEntity, String.class);String body = responseEntity.getBody();System.err.println(body);}二、發送get請求不帶headers
@Testpublic void testGetWithoutHeaders(){Map<String,Integer> map = new HashMap<String,Integer>();map.put("pageNum",1);map.put("pageSize",15);//get請求String url = "https://xxxxxx.com:8101/operate/operate_pay/getOperatesPay?pageNum={pageNum}&pageSize={pageSize}";ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, map);String body = responseEntity.getBody();System.err.println(body);}三、發送post請求攜帶headers
@Testpublic void testPostWithHeaders(){String url = "https://XXXXX.com:8101/baseinfo/company/setCompany";String json = "{\n" +"\t\"contactAddress\":\"武漢市洪山區郵科院內東區烽火創新谷精密工藝樓5樓5區\",\n" +"\t\"legalName\":\"XXXX\",\n" +"\t\"companyId\":\"XXXXX\",\n" +"\t\"address\":420100,\n" +"\t\"businessScope\":\"網絡預約出租汽車客運\",\n" +"\t\"economicType\":\"有限責任(公司)\",\n" +"\t\"companyName\":\"武漢XXXXXX信息科技有限公司\",\n" +"\t\"identifier\":\"XXXXXX\",\n" +"\t\"legalPhone\":\"XXXXXX\",\n" +"\t\"regCapital\":\"1000000.0000萬元\",\n" +"\t\"state\":0,\n" +"\t\"flag\":1,\n" +"\t\"legalID\":\"XXXXXXXXXXXXXXX\",\n" +"\t\"updateTime\":20180920111808,\n" +"\t\"legalPhoto\":\"driver/drivelicense1_1524554483.jpg\"\n" +"}";HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);JSONObject jsonObject = JSONObject.parseObject(json);// Map map =JSONObject.parseObject(json, Map.class);// MultiValueMap<String,Object> multiValueMap = new LinkedMultiValueMap<>();// multiValueMap.setAll(map);HttpEntity httpEntity = new HttpEntity<>(jsonObject,headers);// ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, map, String.class,httpEntity);ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);System.err.println(responseEntity.getBody());}四、發送post請求不帶headers
@Testpublic void testPostWithoutHeaders(){String url = "https://XXXXX.com:8101/baseinfo/company/setCompany";String json = "{\n" +"\t\"contactAddress\":\"武漢市洪山區郵科院內東區烽火創新谷精密工藝樓5樓5區\",\n" +"\t\"legalName\":\"XXXX\",\n" +"\t\"companyId\":\"42XX01XXXX4LXXX9C\",\n" +"\t\"address\":420100,\n" +"\t\"businessScope\":\"網絡預約出租汽車客運\",\n" +"\t\"economicType\":\"有限責任(公司)\",\n" +"\t\"companyName\":\"武漢XXXXXXX信息科技有限公司\",\n" +"\t\"identifier\":\"XXXXXXX\",\n" +"\t\"legalPhone\":\"XXXXXXX\",\n" +"\t\"regCapital\":\"100000.0000萬元\",\n" +"\t\"state\":0,\n" +"\t\"flag\":1,\n" +"\t\"legalID\":\"XXXXXXXX\",\n" +"\t\"updateTime\":20180920111808,\n" +"\t\"legalPhoto\":\"driver/drivelicense1_1524554483.jpg\"\n" +"}";Map map = JSONObject.parseObject(json, Map.class);ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, map, String.class);System.err.println(responseEntity.getBody());}補充:配置類。視情況而選擇,也可使用默認的配置。
/**** 內部服務調用者* @author zhangxueliang*/ @Configuration public class RestTemplateConfig {/*** 適用于服務使用RestTemplate調用外部地址請求* @return*/@Beanpublic RestTemplate restTemplate() {RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());return restTemplate ;}/**** 適用于服務內部之間使用RestTemplate相互調用增加Rinbbo機制* 通過服務名方式* @return*/@Bean(name="loadBalancedRestTemplate")@LoadBalancedpublic RestTemplate loadBalancedRestTemplate() {RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());return restTemplate ;}private ClientHttpRequestFactory clientHttpRequestFactory() {HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();factory.setReadTimeout(5000);factory.setConnectTimeout(5000);factory.setConnectionRequestTimeout(5000);return factory;}}?
總結
以上是生活随笔為你收集整理的RestTemplate带参发送get及post请求携带headers请求头代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RestTemplate设置通用head
- 下一篇: Spring Boot连接多个Redis