用RestTemple是需要对应的配置文件的,这里就不放了
@Autowired
private RestTemplate restTemplate;
public JSONObject handle(String url){
System.out.println("调用数据接口");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Accept", "application/json");//请求头
headers.add("appId", "6325b8c0548b64716c9091c44b4b0bb3");
headers.add("userName", "yyzg");
headers.add("passWord", "000000");
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
HttpEntity<MultiValueMap> request = new HttpEntity<MultiValueMap>(map, headers);
JSONObject response = restTemplate.postForEntity(url, request, JSONObject.class).getBody();
System.out.println(response);
return response;
}
如果要做分页
@Autowired
private RestTemplate restTemplate;
public JSONObject handle(String url,Integer pageIndex,Integer pageSize){
System.out.println("调用数据接口");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Accept", "application/json");//请求头
headers.add("appId", "6325b8c0548b64716c9091c44b4b0bb3");
headers.add("userName", "yyzg");
headers.add("passWord", "000000");
JSONObject json=new JSONObject();
json.put("pageIndex",pageIndex);
json.put("pageSize",pageSize);
HttpEntity<String> entity = new HttpEntity<>(JSONObject.toJSONString(json),headers);
JSONObject response= restTemplate.postForObject(url, entity,JSONObject.class);
return response;
}