请求远程接口 get方式
/**
* 请求时将请求路径与参数拼装好放入url中传入
*示例url:InterfaceRequestGet("http://192.168.100.100:121/Xxx/getxxxx?key1=%s&key2=%s,xxx1,xxx2")
*/
public List<Map<String,Object>> InterfaceRequestGet(String url){
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
log.debug("request:"+url);
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
String str=result.getBody();//接口的返回值要以具体的实际为准去解析
log.debug("response:"+str);
str=str.substring(str.indexOf("{"),str.lastIndexOf(("}"))+1);
JSONObject resultJson = JSONObject.parseObject(str);
JSONArray jsonArray=resultJson.getJSONArray("data");
List<Map<String,Object>> mapList = (List<Map<String,Object>>) JSONArray.parse(jsonArray.toString());
return mapList;
}
请求远程接口 post方式
/**
* 请求时将请求路径与参数分开
*示例:InterfaceRequest("192.168.100.100:121/Xxx/getxxxx", requestBody);
*/
public String InterfaceRequest(String url, LinkedMultiValueMap<String,String> requestBody){
//String url="http://192.168.100.202:8081/LesaseQuery.asmx/LeaseQueryBYFWBH";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> httpEntity =
new HttpEntity<MultiValueMap<String, String>>(requestBody, headers);
log.debug("request:"+url);
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
String str=result.getBody();//接口的返回值要以具体的实际为准去解析
log.debug("response:"+str);
str=str.substring(str.indexOf("{"),str.lastIndexOf(("}"))+1);
return str;
}