本文是精讲RestTemplate第3篇,前篇的blog拜访地址如下:
- 精讲RestTemplate第1篇-在Spring或非Spring环境下如何应用
- 精讲RestTemplate第2篇-多种底层HTTP客户端类库的切换
RestTemplate能够发送HTTP GET申请,常常应用到的办法有两个:
- getForObject()
- getForEntity()
二者的次要区别在于,getForObject()返回值是HTTP协定的响应体。getForEntity()返回的是ResponseEntity,ResponseEntity是对HTTP响应的封装,除了蕴含响应体,还蕴含HTTP状态码、contentType、contentLength、Header等信息。
为了不便后续开发测试,首先介绍一个网站给大家。JSONPlaceholder是一个提供收费的在线REST API的网站,咱们在开发时能够应用它提供的url地址测试下网络申请以及申请参数。或者当咱们程序须要获取一些模仿数据、模仿图片时也能够应用它。
一、 getForObject() 办法
1.1.以String的形式承受申请后果数据
在Spring Boot环境下写一个单元测试用例,以String类型接管响应后果信息
@SpringBootTest
class ResttemplateWithSpringApplicationTests {
@Resource
private RestTemplate restTemplate;
@Test
void testSimple() {
String url = "http://jsonplaceholder.typicode.com/posts/1";
String str = restTemplate.getForObject(url, String.class);
System.out.println(str);
}
}
getForObject第二个参数为返回值的类型,String.class以字符串的模式承受getForObject响应后果,
1.2.以POJO对象的形式承受后果数据
在Spring Boot环境下写一个单元测试用例,以java POJO对象接管响应后果信息
@Test
public void testPoJO() {
String url = "http://jsonplaceholder.typicode.com/posts/1";
PostDTO postDTO = restTemplate.getForObject(url, PostDTO.class);
System.out.println(postDTO.toString());
}
输入打印后果如下:
POJO的定义如下,依据JSON String的数据格式定义。
@Data
public class PostDTO {
private int userId;
private int id;
private String title;
private String body;
}
1.3.以数组的形式接管申请后果
拜访http://jsonplaceholder.typicode.com/posts 能够取得JSON数组形式的申请后果
下一步就是咱们该如何接管,应用办法也很简略。在Spring Boot环境下写一个单元测试用例,以数组的形式接管申请后果。
@Test
public void testArrays() {
String url = "http://jsonplaceholder.typicode.com/posts";
PostDTO[] postDTOs = restTemplate.getForObject(url, PostDTO[].class);
System.out.println("数组长度:" + postDTOs.length);
}
申请的后果被以数组的形式正确接管,输入如下:
数组长度:100
1.4.应用占位符号传参的几种形式
以下的几个申请都是在拜访”http://jsonplaceholder.typicode.com/posts/1″,只是应用了占位符语法,这样在业务应用上更加灵便。
- 应用占位符的模式传递参数:
String url = "http://jsonplaceholder.typicode.com/{1}/{2}";
PostDTO postDTO = restTemplate.getForObject(url, PostDTO.class, "posts", 1);
- 另一种应用占位符的模式:
String url = "http://jsonplaceholder.typicode.com/{type}/{id}";
String type = "posts";
int id = 1;
PostDTO postDTO = restTemplate.getForObject(url, PostDTO.class, type, id);
- 咱们也能够应用 map 装载参数:
String url = "http://jsonplaceholder.typicode.com/{type}/{id}";
Map<String,Object> map = new HashMap<>();
map.put("type", "posts");
map.put("id", 1);
PostDTO postDTO = restTemplate.getForObject(url, PostDTO.class, map);
二、getForEntity()办法
下面的所有的getForObject申请传参办法,getForEntity都能够应用,应用办法上也简直是统一的,只是在返回后果接管的时候略有差异。应用ResponseEntity<T> responseEntity
来接管响应后果。用responseEntity.getBody()获取响应体。响应体内容同getForObject办法返回后果统一。剩下的这些响应信息就是getForEntity比getForObject多进去的内容。
HttpStatus statusCode = responseEntity.getStatusCode();
获取整体的响应状态信息int statusCodeValue = responseEntity.getStatusCodeValue();
获取响应码值HttpHeaders headers = responseEntity.getHeaders();
获取响应头- 等
@Test
public void testEntityPoJo() {
String url = "http://jsonplaceholder.typicode.com/posts/5";
ResponseEntity<PostDTO> responseEntity
= restTemplate.getForEntity(url, PostDTO.class);
PostDTO postDTO = responseEntity.getBody(); // 获取响应体
System.out.println("HTTP 响应body:" + postDTO.toString());
//以下是getForEntity比getForObject多进去的内容
HttpStatus statusCode = responseEntity.getStatusCode(); // 获取响应码
int statusCodeValue = responseEntity.getStatusCodeValue(); // 获取响应码值
HttpHeaders headers = responseEntity.getHeaders(); // 获取响应头
System.out.println("HTTP 响应状态:" + statusCode);
System.out.println("HTTP 响应状态码:" + statusCodeValue);
System.out.println("HTTP Headers信息:" + headers);
}
输入打印后果
欢送关注我的博客,外面有很多精品合集
- 本文转载注明出处(必须带连贯,不能只转文字):字母哥博客。
感觉对您有帮忙的话,帮我点赞、分享!您的反对是我不竭的创作能源! 。另外,笔者最近一段时间输入了如下的精品内容,期待您的关注。
- 《手摸手教你学Spring Boot2.0》
- 《Spring Security-JWT-OAuth2一本通》
- 《实战前后端拆散RBAC权限管理系统》
- 《实战SpringCloud微服务从青铜到王者》
- 《VUE深入浅出系列》
发表回复