RestTemplate GET请求怎么用(get,resttemplate,开发技术)

时间:2024-05-09 14:12:21 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

一、 getForObject() 方法

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

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

@SpringBootTestclassResttemplateWithSpringApplicationTests{@ResourceprivateRestTemplaterestTemplate;@TestvoidtestSimple(){Stringurl="http://jsonplaceholder.typicode.com/posts/1";Stringstr=restTemplate.getForObject(url,String.class);System.out.println(str);}}

getForObject第二个参数为返回值的类型,String.class以字符串的形式接受getForObject响应结果,

RestTemplate GET请求怎么用

1.2.以POJO对象的方式接受结果数据

在Spring Boot环境下写一个单元测试用例,以java POJO对象接收响应结果信息

@TestpublicvoidtestPoJO(){Stringurl="http://jsonplaceholder.typicode.com/posts/1";PostDTOpostDTO=restTemplate.getForObject(url,PostDTO.class);System.out.println(postDTO.toString());}

输出打印结果如下:

RestTemplate GET请求怎么用

POJO的定义如下,根据JSON String的数据格式定义。

@DatapublicclassPostDTO{privateintuserId;privateintid;privateStringtitle;privateStringbody;}

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

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

RestTemplate GET请求怎么用

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

@TestpublicvoidtestArrays(){Stringurl="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",只是使用了占位符语法,这样在业务使用上更加灵活。

使用占位符的形式传递参数:

Stringurl="http://jsonplaceholder.typicode.com/{1}/{2}";PostDTOpostDTO=restTemplate.getForObject(url,PostDTO.class,"posts",1);

另一种使用占位符的形式:

Stringurl="http://jsonplaceholder.typicode.com/{type}/{id}";Stringtype="posts";intid=1;PostDTOpostDTO=restTemplate.getForObject(url,PostDTO.class,type,id);

我们也可以使用 map 装载参数:

Stringurl="http://jsonplaceholder.typicode.com/{type}/{id}";Map<String,Object>map=newHashMap<>();map.put("type","posts");map.put("id",1);PostDTOpostDTO=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();获取响应头等

@TestpublicvoidtestEntityPoJo(){Stringurl="http://jsonplaceholder.typicode.com/posts/5";ResponseEntity<PostDTO>responseEntity=restTemplate.getForEntity(url,PostDTO.class);PostDTOpostDTO=responseEntity.getBody();//获取响应体System.out.println("HTTP响应body:"+postDTO.toString());//以下是getForEntity比getForObject多出来的内容HttpStatusstatusCode=responseEntity.getStatusCode();//获取响应码intstatusCodeValue=responseEntity.getStatusCodeValue();//获取响应码值HttpHeadersheaders=responseEntity.getHeaders();//获取响应头System.out.println("HTTP响应状态:"+statusCode);System.out.println("HTTP响应状态码:"+statusCodeValue);System.out.println("HTTPHeaders信息:"+headers);}

输出打印结果

RestTemplate GET请求怎么用

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:RestTemplate GET请求怎么用的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:微信小程序常用表单组件如何使用下一篇:

18 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18