一、RestTemplate是什么
RestTemplate是spring-web-xxx.jar包中提供的Http协议实现类。也就是说导入spring-boot-starter-web的项目可以直接使用RestTemplate类,就是基于模板方法设计模式的,封装了所有需要使用的API
3.1 GET请求
3.1.1 getForEntity
3.1.2 getForObject
3.2 POST请求
3.2.1 postForEntity
3.2.3 postForObject
3.2.3 postForLocation
3.3 PUT请求
3.4 DELETE请求
3.5 补充
3.5.1 exchange方法
3.5.2 execute方法
在该类中主要针对6类请求方式封装的方法。
1.1 说明
get方式提供了两个方法:
两个方法都是发送get请求并处理响应。区别:
getForObject:把响应体直接转换为对象。该方法返回值为特定类类型。舍弃了Response Header的东西,但是用起来比getForEntity方便。如果只需要获取响应体中内容(调用控制器方法的返回值)使用此方法。
getForEntity:返回值包含响应头和响应体。用起来比getForObject稍微麻烦一些。
/**
* @url : 路径
* @responseType : 返回消息体的类
* @uriVariables :可变长度参数
**/
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables)
/**
* @url : 路径
* @responseType : 返回消息体的类
* @uriVariables :参数map
**/
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables
/**
* @url : 使用UriComponents构建的路径
* @responseType : 返回消息体的类
**/
<T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType)
//获取返回数据的body
responseEntity.getBody();
//获取状态码
responseEntity.getStatusCode();
//获取状态码的值
responseEntity.getStatusCodeValue();
//获取请求头
responseEntity.getHeaders()
/**
* @url : 路径
* @responseType : 返回对象的类
* @uriVariables :可变长度参数
**/
<T> T getForObject(String url, Class<T> responseType, Object... uriVariables)
/**
* @url : 路径
* @responseType : 返回对象的类
* @uriVariables :参数map
**/
<T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables
/**
* @url : 使用UriComponents构建的路径
* @responseType : 返回对象的类
**/
<T> T getForObject(URI url, Class<T> responseType)
RestTemplate及HttpClient_resttemplate jar包_jsxllht的博客-CSDN博客
Spring框架(七)——RestTemplate_resttemplate是什么_jc_hook的博客-CSDN博客