RestTemplate中getForEntity()方法的示例分享
下文笔者讲述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);
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。