RestTemplate使用详解
程序员文章站
2022-04-28 18:32:22
...
简述RestTemplate:
是Spring用于同步client端的核心类,简化了与http服务的通信,并满足RestFul原则,程序代码可以给它提供URL,并提取结果。默认情况下,RestTemplate默认依赖jdk的HTTP连接工具。当然你也可以 通过setRequestFactory属性切换到不同的HTTP源,比如Apache HttpComponents、Netty和OkHttp。
getForObject()方法
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables){}
public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables)
public <T> T getForObject(URI url, Class<T> responseType)
//provider
@GetMapping("/addUser/{userid}/{username}")
public Map<String,Object> addUser(@PathVariable("userid")Integer userid,
@PathVariable("username") String username){
Map<String,Object> map = new HashMap<>();
map.put("userid",userid);
map.put("username",username);
return map;
}
//consumer
//方式1:直接在url后拼接值
@GetMapping("/getForObject1")
public Map<String,Object> getForObject1(){
Map map = restTemplate.getForObject("http://127.0.0.1:8080/addUser/100/张三", Map.class);
return map;
}
//consumer
//方式2:使用{}进行占位
@GetMapping("/getForObject2")
public Map<String,Object> getForObject2(){
//url,返回值类型,url路径参数
Map map = restTemplate.getForObject("http://127.0.0.1:9090/addUser/{1}/{2}", Map.class,100,"李四");
return map;
}
//consumer
//方式3:参数为map,占位的名字要和map中的key进行对应
@GetMapping("/getForObject3")
public Map<String,Object> getForObject3(){
Map<String, Object> params = new HashMap<>();
params.put("userid",99);
params.put("username","王五");
//map中的key和url中的参数一一对应
Map map = restTemplate.getForObject("http://127.0.0.1:9090/addUser/{userid}/{username}", Map.class,params);
return map;
}
getForEntity()方法
public <T> T getForEntity(String url, Class<T> responseType, Object... uriVariables){}
public <T> T getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables)
public <T> T getForEntity(URI url, Class<T> responseType)
getForEntity()用法和getForObject相同,但是getForEntity的返回值是一个ResponseEntity,ResponseEntity是Spring对HTTP请求响应的封装,包含了几个重要元素,如响应码,contentType,contentLength,响应体等。
postForObject()方法
public <T> T getForObject(String url, Object request, Class<T> responseType, Object... uriVariables){}
public <T> T getForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
public <T> T getForObject(URI url, Object request, Class<T> responseType)
对象作为参数
//provider
@PostMapping("/user")
public String addUser(@RequestBody UserVo userVo){
//userVo直接就是对象
System.out.println(userVo);
return "OK";
}
//consumer
@RequestMapping("/add")
public String add(){
HttpHeaders headers = new HttpHeaders();
//设置请求内容为json
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//创建请求实体对象
UserVo userVo = new UserVo();
userVo.setId(111);
userVo.setName("王五");
HttpEntity<UserVo> request = new HttpEntity<>(userVo, headers);
String s = restTemplate.postForObject("http://127.0.0.1:9090/user", request, String.class);
return s;
}
map类型作为参数
//provider
@PostMapping("/user2")
public String addUser(@RequestBody Map<String,Object> map){
//注意: map中获取的是json,需要自己转换成对象
Object token = map.get("token");
Object userVo = map.get("userVo");
System.out.println(userVo);
System.out.println(token);
return "OK";
}
//consumer
@RequestMapping("/add2")
public String add2(){
HttpHeaders headers = new HttpHeaders();
//设置请求内容为json
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//创建请求实体对象
UserVo userVo = new UserVo();
userVo.setId(111);
userVo.setName("王五");
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("userVo",userVo);
map.add("token","asedfasdfase");
HttpEntity<LinkedMultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
String s = restTemplate.postForObject("http://127.0.0.1:9090/user2", request, String.class);
return s;
}
postForEntity()方法
public <T> T getForEntity(String url, Object request, Class<T> responseType, Object... uriVariables){}
public <T> T getForEntity(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
public <T> T getForEntity(URI url, Object request, Class<T> responseType)
postForEntity()用法和getForObject相同,但是psotForEntity的返回值是一个ResponseEntity,ResponseEntity是Spring对HTTP请求响应的封装,包含了几个重要元素,如响应码,contentType,contentLength,响应体等。
上一篇: 1024程序员节~