Spring boot 使用RestTemplate调用第三方
当Content-type为“application/x-www-form-urlencode”时
- 请求体参数传值方式
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type","application/x-www-form-urlencoded");
MultiValueMap param = new LinkedMultiValueMap();
param.add("abc","abc");
HttpEntity httpEntity = new HttpEntity<>(param,headers);
ResponseEntity<JSONObject> entity = RestTemplateUtil.post(url,httpEntity, JSONObject.class);
- 请求路径传值也就是url传值
GET
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type","application/x-www-form-urlencoded");
UriComponentsBuilder builder = UriComponentsBuilder
.fromUriString(url)
.queryParam("pageNum", 2)
.queryParam("pageSize",10);
HttpEntity entity = new HttpEntity<>(headers);
ResponseEntity<JSONObject> res = RestTemplateUtil.get(builder.toUriString(), headers, JSONObject.class);
POST
//格式一
String url = "https://abc/{id}/{name}";
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
Map map = new HashMap();
map.put("id",123);
map.put("name","zhang");
HttpEntity entity = new HttpEntity<>(headers);
ResponseEntity<JSONObject> res = RestTemplateUtil.exchange(url,HttpMethod.POST,entity,JSONObject.class,map);
//格式二
String url = "https://abc?PageSize=10&pageNum=1&name=zhang";
UriComponentsBuilder builder = UriComponentsBuilder
.fromUriString(url)
.queryParam("name","zhang")
.queryParam("PageSize",10)
.queryParam("CurPage",1);
ResponseEntity<String> res = RestTemplateUtil.get(builder.toUriString(), String.class);
当Content-type为“application/json”时
- 请求体参数传值
String url = "https://abcde/authorize";
Map headers = new HashMap();
headers.put("Content-Type","application/json");
//参数body json格式
JSONObject jsonO = new JSONObject();
jsonO.put("id",id);
jsonO.put("name",name);
jsonO.put("openId",openId);
ResponseEntity<JSONObject> res = RestTemplateUtil.post(url,headers,jsonO,JSONObject.class);
System.out.println(res.getBody());
公共类
package xxx.xxx.xxx.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Slf4j
public class RestTemplateUtil {
private static final RestTemplate restTemplate = new RestTemplate();
// ----------------------------------GET-------------------------------------------------------
/**
* GET请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, Class<T> responseType) {
return restTemplate.getForEntity(url, responseType);
}
/**
* GET请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, Class<T> responseType, Object... uriVariables) {
return restTemplate.getForEntity(url, responseType, uriVariables);
}
/**
* GET请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, Class<T> responseType, Map<String, ?> uriVariables) {
return restTemplate.getForEntity(url, responseType, uriVariables);
}
/**
* 带请求头的GET请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, Map