RestTemplate GET请求怎么用

2023-06-09,

这篇文章给大家分享的是有关RestTemplate GET请求怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、 getForObject() 方法

1.1.以String的方式接受请求结果数据

在Spring Boot环境下写一个单元测试用例,以String类型接收响应结果信息

@SpringBootTestclass 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对象接收响应结果信息

@Testpublic 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的数据格式定义。

@Datapublic class PostDTO {    private int userId;    private int id;    private String title;    private String body;}

1.3.以数组的方式接收请求结果

访问http://jsonplaceholder.typicode.com/posts 可以获得JSON数组方式的请求结果

下一步就是我们该如何接收,使用方法也很简单。在Spring Boot环境下写一个单元测试用例,以数组的方式接收请求结果。

@Testpublic 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();获取响应头等

@Testpublic 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);}

输出打印结果

感谢各位的阅读!关于“RestTemplate GET请求怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

《RestTemplate GET请求怎么用.doc》

下载本文的Word格式文档,以方便收藏与打印。