关于spring:精讲RestTemplate第3篇GET请求使用方法详解

4次阅读

共计 3457 个字符,预计需要花费 9 分钟才能阅读完成。

本文是精讲 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 深入浅出系列》
正文完
 0