在pom.xml添加以下依赖项 com.alibaba.fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
直接上代码
public static JSONObject sendPost(String url, Map<String, String> data, Map<String, String> headerData) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
for (Map.Entry<String, String> entry : headerData.entrySet()) {
headers.add(entry.getKey(), entry.getValue());
}
MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
param.setAll(data);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(param, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
return JSONObject.parseObject(response.getBody());
}
public static JSONObject sendGet(String url,Map<String, String> data){
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.getForEntity(url,String.class,data);
return JSONObject.parseObject(response.getBody());
}
|