@Test
public void testGetWithHeaders(){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Map map = new HashMap();
map.put("pageNum",1);
map.put("pageSize",15);
HttpEntity httpEntity = new HttpEntity<>(null, headers);
//get请求
String newUrl = "https://xxxxxxx.com:8101/operate/operate_pay/getOperatesPay?pageNum="+1+"&pageSize="+15;
ResponseEntity responseEntity = restTemplate.exchange(newUrl, HttpMethod.GET, httpEntity, String.class);
String body = responseEntity.getBody();
System.err.println(body);
}
@Test
public void testGetWithoutHeaders(){
Map map = new HashMap();
map.put("pageNum",1);
map.put("pageSize",15);
//get请求
String url = "https://xxxxxx.com:8101/operate/operate_pay/getOperatesPay?pageNum={pageNum}&pageSize={pageSize}";
ResponseEntity responseEntity = restTemplate.getForEntity(url, String.class, map);
String body = responseEntity.getBody();
System.err.println(body);
}
@Test
public 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 multiValueMap = new LinkedMultiValueMap<>();
// multiValueMap.setAll(map);
HttpEntity httpEntity = new HttpEntity<>(jsonObject,headers);
// ResponseEntity responseEntity = restTemplate.postForEntity(url, map, String.class,httpEntity);
ResponseEntity responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
System.err.println(responseEntity.getBody());
}
@Test
public 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 responseEntity = restTemplate.postForEntity(url, map, String.class);
System.err.println(responseEntity.getBody());
}
/***
* 内部服务调用者
* @author zhangxueliang
*/
@Configuration
public class RestTemplateConfig {
/**
* 适用于服务使用RestTemplate调用外部地址请求
* @return
*/
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
return restTemplate ;
}
/***
* 适用于服务内部之间使用RestTemplate相互调用增加Rinbbo机制
* 通过服务名方式
* @return
*/
@Bean(name="loadBalancedRestTemplate")
@LoadBalanced
public 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;
}
}