一、什么是RestTemplate
RestTemplate 是从 Spring3.0 开始支持的一个 HTTP 请求工具,它提供了常见的REST请求方案的模版,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange 以及 execute。RestTemplate 继承自 InterceptingHttpAccessor 并且实现了 RestOperations 接口,其中 RestOperations 接口定义了基本的 RESTful 操作,这些操作在 RestTemplate 中都得到了实现。
传统情况下在java代码里访问Restful服务,一般使用Apache的HttpClient。不过此种方法使用起来太繁琐。Spring提供了一种简单便捷的模板类RestTemplate来进行操作:
定义一个简单的例子:
@RestController
public class TestController
{
@RequestMapping(value = "testPost", method = RequestMethod.POST)
public ResponseBean testPost(@RequestBody RequestBean requestBean)
{
ResponseBean responseBean = new ResponseBean();
responseBean.setRetCode("0000");
responseBean.setRetMsg("succ");
return responseBean;
}
}
使用RestTemplate访问该服务:
//请求地址
String url = "http://localhost:8080/testPost";
//入参
RequestBean requestBean = new RequestBean();
requestBean.setTest1("1");
requestBean.setTest2("2");
requestBean.setTest3("3");
RestTemplate restTemplate = new RestTemplate();
ResponseBean responseBean = restTemplate.postForObject(url, requestBean, ResponseBean.class);
使用restTemplate访问restful接口非常的简单粗暴无脑。(url, requestBean, ResponseBean.class)这三个参数分别代表 请求地址、请求参数、HTTP响应转换被转换成的对象类型。
二、RestTemplate 添加请求头headers和请求体body
//headers & cookie
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("basecret", config.getBasecret());
headers.add("baid", config.getBaid());
List<String> cookies = new ArrayList<>();
cookies.add("COOKIE_USER" + Strings.nullToEmpty(config.getCookie()));
headers.put(HttpHeaders.COOKIE, cookies);
(1)POST请求
1、 调用postForObject方法 2、使用postForEntity方法 3、调用exchange方法
postForObject和postForEntity方法的区别主要在于可以在postForEntity方法中设置header的属性,当需要指定header的属性值的时候,使用postForEntity方法。exchange方法和postForEntity类似,但是更灵活,exchange还可以调用get请求。使用这三种方法传递参数,Map不能定义为以下两种类型
Map<String, Object> paramMap = new HashMap<String, Object>();
Map<String, Object> paramMap = new LinkedHashMap<String, Object>();
把Map类型换成LinkedMultiValueMap后,参数成功传递到后台
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
paramMap.add("dt", "20190225");
// 1、使用postForObject请求接口
String result = template.postForObject(url, paramMap, String.class);
// 2、使用postForEntity请求接口
HttpHeaders headers = new HttpHeaders();
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap,headers);
ResponseEntity<String> response2 = template.postForEntity(url, httpEntity, String.class);
// 3、使用exchange请求接口
ResponseEntity<String> response3 = template.exchange(url, HttpMethod.POST, httpEntity, String.class);
如果post请求体是个Json的表单
//JSONObject userInfo = new JSONObject();
Map<String, Object> userInfo = Maps.newHashMap();
userInfo.put("phone", accountForm.getPhone());
userInfo.put("job", accountForm.getJob());
userInfo.put("email", accountForm.getEmail());
Map<String, Object> postBody = Maps.newHashMap();
postBody.put("userInfo", userInfo);
HttpEntity<Map> requestEntity = new HttpEntity<>(postBody, headers);
try {
ResponseEntity<String> result = restTemplate.postForEntity(config.getCreateWithAuthUrl(), requestEntity, String.class);
JsonNode jsonNode = JsonUtils.toJsonNode(result.getBody());
if (jsonNode.get("errno").asInt() == 200 || jsonNode.get("errno").asInt() == 505) {
return true;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
(2)GET请求
如果是get请求,又想要把参数封装到map里面进行传递的话,Map需要使用HashMap,且url需要使用占位符,如下
RestTemplate restTemplate2 = new RestTemplate();
String url = "http://127.0.0.1:8081/interact/getData?dt={dt}&ht={ht}";
// 封装参数,这里是HashMap
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("dt", "20190225");
paramMap.put("ht", "10");
//1、使用getForObject请求接口
String result1 = template.getForObject(url, String.class, paramMap);
System.out.println("result1====================" + result1);
//2、使用exchange请求接口
HttpHeaders headers = new HttpHeaders();
headers.set("id", "lidy");
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null,headers);
ResponseEntity<String> response2 = template.exchange(url, HttpMethod.GET, httpEntity, String.class,paramMap);
三、请求示例:
1、GET
private static void getEmployees(){
final String uri = "http://localhost:8080/springrestexample/employees";
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);
System.out.println(result);
}
2、使用RestTemplate定制HTTP头文件
private static void getEmployees(){
final String uri = "http://localhost:8080/springrestexample/employees";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
System.out.println(result);
}
3、Get请求获取响应为一个对象
private static void getEmployees(){
final String uri = "http://localhost:8080/springrestexample/employees";
RestTemplate restTemplate = new RestTemplate();
EmployeeListVO result = restTemplate.getForObject(uri, EmployeeListVO.class);
System.out.println(result);
}
4、URL 参数
private static void getEmployeeById(){
final String uri = "http://localhost:8080/springrestexample/employees/{id}";
Map<String, String> params = new HashMap<String, String>();
params.put("id", "1");
RestTemplate restTemplate = new RestTemplate();
EmployeeVO result = restTemplate.getForObject(uri, EmployeeVO.class, params);
System.out.println(result);
}
5、POST
private static void createEmployee(){
final String uri = "http://localhost:8080/springrestexample/employees";
EmployeeVO newEmployee = new EmployeeVO(-1, "Adam", "Gilly", "test@email.com");
RestTemplate restTemplate = new RestTemplate();
EmployeeVO result = restTemplate.postForObject( uri, newEmployee, EmployeeVO.class);
System.out.println(result);
}
6、Submit Form Data
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("id", "1");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
RestTemplate restTemplate = new RestTemplate();
EmployeeVO result = restTemplate.postForObject( uri, request, EmployeeVO.class);
System.out.println(result);
7、PUT
private static void updateEmployee(){
final String uri = "http://localhost:8080/springrestexample/employees/{id}";
Map<String, String> params = new HashMap<String, String>();
params.put("id", "2");
EmployeeVO updatedEmployee = new EmployeeVO(2, "New Name", "Gilly", "test@email.com");
RestTemplate restTemplate = new RestTemplate();
restTemplate.put ( uri, updatedEmployee, params);
}
8、Simple PUT
Foo updatedInstance = new Foo("newName");
updatedInstance.setId(createResponse.getBody().getId());
String resourceUrl =
fooResourceUrl + '/' + createResponse.getBody().getId();
HttpEntity<Foo> requestUpdate = new HttpEntity<>(updatedInstance, headers);
template.exchange(resourceUrl, HttpMethod.PUT, requestUpdate, Void.class);
使用.exchange和请求回调
RequestCallback requestCallback(final Foo updatedInstance) {
return clientHttpRequest -> {
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(clientHttpRequest.getBody(), updatedInstance);
clientHttpRequest.getHeaders().add(
HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
clientHttpRequest.getHeaders().add(
HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedLogPass());
};
}
9、DELETE
private static void deleteEmployee(){
final String uri = "http://localhost:8080/springrestexample/employees/{id}";
Map<String, String> params = new HashMap<String, String>();
params.put("id", "2");
RestTemplate restTemplate = new RestTemplate();
restTemplate.delete ( uri, params );
}
10、发送带header的Post请求
// header填充
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.put("Content-Type", Collections.singletonList("application/json;charset=UTF-8"));
headers.put("signature", Collections.singletonList(makeSignature(form.getNewMobile())));
// body填充
JSONObject json = new JSONObject();
json.put("oldMobile", mobile);
json.put("newMobile", form.getNewMobile());
HttpEntity<String> request = new HttpEntity<String>(json.toString(), headers);
// 一个单例的restTemplate
RestTemplate restTemplate = HttpInvoker.getRestTemplate();
// 发送请求
ResponseEntity<String> response = restTemplate.postForEntity(whiteListURL, request, String.class);
String url = prometheusServer + api;
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
// 查询参数
map.add("query", sql.toString());
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
// 接受响应的类要与 接口响应一致,不然报错
ResponseEntity<PostResultVo> exchange = restTemplate.postForEntity(url, request, PostResultVo.class);
if (null != exchange && null != exchange.getBody() && null != exchange.getBody().getData()) {
}
11、发送带header的Get请求
/**
* 带header的GET请求
*/
@Test
public void getHasHeader() {
long userId = 32L;
HttpHeaders headers = new HttpHeaders();
headers.add("token", "123");
ResponseEntity<UserBean> response = restTemplate.exchange(
"http://127.0.0.1:8280/user/{id}",
HttpMethod.GET,
new HttpEntity<String>(headers),
UserBean.class,
userId);
logger.info("response={}", JSON.toJSONString(response.getBody()));
}
private static void getEmployees(){
final String uri = "http://localhost:8080/springrestexample/employees";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
System.out.println(result);
}