springboot中的restTemplate访问get,post请求的各种方式
springboot中封装好了访问外部请求的方法类,那就是RestTemplate。下面就简单介绍一下,RestTemplate访问外部请求的方法。
get请求
首先get请求的参数是拼接在url后面的。所以不需要额外添加参数。但是也需要分两种情况。
1、 有请求头
由于 getForEntity() 和 getForObject() 都无法加入请求头。所以需要请求头的连接只能使用 exchange() 来访问。代码如下
public JSONObject test(){
try {
RestTemplate re = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
String url = "http://test.api.com?id=123";
headers.set("Content-Type","application/json");
HttpEntity<JSONObject> jsonObject= re.exchange(url, HttpMethod.GET,new HttpEntity<>(headers),JSONObject.class);
log.info("返回:{}",jsonObject.getBody());
return jsonObject.getBody();
}catch (Exception e){
log.error(e.getMessage());
}
return null;
}
2、 无请求头
无需请求头的可以用三个方法实现。getForEntity() 和 getForObject() 还有 exchange() 都可以实现。下面讲前两种用的比较多的。
1、getForEntity()
public JSONObject test(){
try {
RestTemplate re = new RestTemplate();
String url = "http://api.help.bj.cn/apis/alarm/?id=101020100";
HttpEntity<JSONObject> jsonObject= re.getForEntity(url,JSONObject.class);
log.info("返回:{}",jsonObject.getBody());
return jsonObject.getBody();
}catch (Exception e){
log.error(e.getMessage());
}
return null;
}
1、getForObject()
public JSONObject test(){
try {
RestTemplate re = new RestTemplate();
String url = "http://api.help.bj.cn/apis/alarm/?id=101020100";
JSONObject jsonObject= re.getForObject(url,JSONObject.class);
log.info("返回:{}",jsonObject);
return jsonObject;
}catch (Exception e){
log.error(e.getMessage());
}
return null;
}
post请求
post请求也分几种情况
1、 参数在body的form-data里面
public static JSONObject test(){
try {
RestTemplate re = new RestTemplate();
String url = "http://localhost:8101/test";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> loginJson = new LinkedMultiValueMap<>();
loginJson.add("id", "123");
JSONObject jsonObject= re.postForObject(url,new HttpEntity<>(loginJson,headers),JSONObject.class);
log.info("返回:{}",jsonObject);
return jsonObject;
}catch (Exception e){
log.error(e.getMessage());
}
return null;
}
还可以把 postForObject 换成 postForEntity
1、 参数在body的x-www-from-urlencodeed里面
只需要把请求头的setContentType改成下面即可headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
public static JSONObject test(){
try {
RestTemplate re = new RestTemplate();
String url = "http://localhost:8101/test";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, Object> loginJson = new LinkedMultiValueMap<>();
loginJson.add("id", "123");
JSONObject jsonObject= re.postForObject(url,new HttpEntity<>(loginJson,headers),JSONObject.class);
log.info("返回:{}",jsonObject);
return jsonObject;
}catch (Exception e){
log.error(e.getMessage());
}
return null;
}
1、 参数在body的raw里面
public static JSONObject test(){
try {
RestTemplate re = new RestTemplate();
String url = "http://localhost:8101/test";
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","application/json");
JSONObject jsonObject = new JSONObject();
jsonObject.put("id","1");
JSONObject jsonObject1 = restTemplate
.postForObject(url,new HttpEntity<>(jsonObject,headers),JSONObject.class);
log.info("返回:{}",jsonObject1);
return jsonObject;
}catch (Exception e){
log.error(e.getMessage());
}
return null;
}