RestTemplate中getForEntity()方法的示例分享

书欣 Spring 发布时间:2023-01-02 17:24:31 阅读数:6684 1
下文笔者讲述getForEntity方法的功能简介说明,如下所示

getForEntity方法简介

getForEntity用于向一个url进行get请求
  此方法请求后会返回整个http请求的信息

getForEntity方法语法

 
public <T> ResponseEntity<T> getForEntity(String url, 
       Class<T> responseType, Object... uriVariables){}
public <T> ResponseEntity<T> getForEntity(String url, 
       Class<T> responseType, Map<String, ?> uriVariables){}
public <T> ResponseEntity<T> getForEntity(URI url,
       Class<T> responseType){}

不带参数的Get请求

String url = "http://localhost:8080/testFunForEntity/";
ResponseEntity<Student> responseEntity = restTemplate.getForEntity(url , Student.class);
responseEntity.getStatusCode();
responseEntity.getHeaders();
responseEntity.getBody();
log.info("【responseEntity : {}】" , responseEntity);

带参数的Get请求(按{}顺序绑定参数)

String url = "http://localhost:8080/testFunForEntity/{name}/{age}";
ResponseEntity<Student> responseEntity = restTemplate.getForEntity(url ,
      Student.class , "java265-name" , "18");
log.info("【responseEntity : {}】" , responseEntity);

带参数的Get请求(按Ma的keyp绑定{}参数)

String url = "http://localhost:9999/testFunForEntity/{name}/{age}";
   Map<String,String> map = new HashMap<>();
map.put("name","java");
map.put("age" , "18");
ResponseEntity<Student> responseEntity = restTemplate.getForEntity(url , Student.class ,"map");
log.info("【responseEntity : {}】" , responseEntity);
 
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/Spring/202301/5256.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者