springboot学习总结(三)RestTemplate用法
(一)配置类
package com.vincent.demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory) { return new RestTemplate(factory); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory() { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(5000); factory.setConnectTimeout(15000); return factory; } }
(二)准备的VO类
package com.vincent.demo.vo; public class TestVO { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "TestVO{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
(三)get请求
1、getForObject
官方提供了如下三个方法
(1)public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException {}
(2)public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException {}
(3)public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {}
(1)无參get请求
controller方法如下
@GetMapping("/getTest") public String getTest() { return "getTest"; }
测试方法
String msg = restTemplate.getForObject("http://localhost:8080/getTest", String.class); System.out.println(msg);
控制台打印信息:
getTest
(2)有参数的get请求
controller方法:
@GetMapping("/urlWithParam/{id}/{name}") public String urlWithParam(@PathVariable Long id, @PathVariable String name) { return "id:" + id + ",name:" + name; }
测试方法:
String msg1 = restTemplate.getForObject("http://localhost:8080/urlWithParam/{id}/{name}", String.class, 1, "vincent"); System.out.println(msg1);
控制台打印信息:
id:1,name:vincent
(3)有参数的get请求
controller方法:
@GetMapping("/getWithVo") public String getWithVo(TestVO testVO) { return testVO.toString(); }
测试方法:
Map<String, String> map = new HashMap(); map.put("id", "1"); map.put("name", "vincent"); String msg2 = restTemplate.getForObject("http://localhost:8080/urlWithParam/{id}/{name}", String.class, map); System.out.println("msg2:" + msg2);
控制台打印信息:
msg2:id:1,name:vincent
(四)有参数的get请求
这一种的情况个人认为比较特殊,用的是无參的<T> T getForObject(URI url, Class<T> responseType),也许是我没找到合适的方式来调用,后期找到了再更新
controller方法:
@GetMapping("/getWithParam") public String getWithParam(@RequestParam Long id, @RequestParam String name) { return "id:" + id + ",name:" + name; }
测试方法:
String msg3 = restTemplate.getForObject("http://localhost:8080/getWithParam?id=1&name=vincent", String.class); System.out.println("msg3:" + msg3);
控制台打印信息:
msg3:id:1,name:vincent
2、getForEntity
官方提供了如下方法:
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException {} public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {} public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException {}
除了返回类型,基本和getForObject一样。
这边举一个无參的请求作为例子
controller方法:
@GetMapping("/getTest") public String getTest() { return "getTest"; }
测试方法:
ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:8080/getTest", String.class); System.out.println("body:" + entity.getBody() + ",StatusCode:" + entity.getStatusCode() + ",StatusCodeValue:" + entity.getStatusCodeValue() + ",:Headers" + entity.getHeaders());
控制台打印:
body:getTest,StatusCode:200,StatusCodeValue:200,:Headers{Content-Type=[text/plain;charset=UTF-8], Content-Length=[7], Date=[Wed, 09 Jan 2019 15:09:04 GMT]}
小结:
getForEntity和getForObject的区别其实就在于返回值,getForEntity的返回值包含http的一些信息,而getForObject就只有接口返回的信息。
这一点看他的源码很容易明白
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException { RequestCallback requestCallback = this.acceptHeaderRequestCallback(responseType); ResponseExtractor<ResponseEntity<T>> responseExtractor = this.responseEntityExtractor(responseType); return (ResponseEntity)this.execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables); } public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException { RequestCallback requestCallback = this.acceptHeaderRequestCallback(responseType); ResponseExtractor<ResponseEntity<T>> responseExtractor = this.responseEntityExtractor(responseType); return (ResponseEntity)this.execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables); } public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException { RequestCallback requestCallback = this.acceptHeaderRequestCallback(responseType); ResponseExtractor<ResponseEntity<T>> responseExtractor = this.responseEntityExtractor(responseType); return (ResponseEntity)this.execute(url, HttpMethod.GET, requestCallback, responseExtractor); } public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException { RequestCallback requestCallback = this.acceptHeaderRequestCallback(responseType); HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor(responseType, this.getMessageConverters(), this.logger); return this.execute(url, HttpMethod.GET, requestCallback, responseExtractor, (Object[])uriVariables); } public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException { RequestCallback requestCallback = this.acceptHeaderRequestCallback(responseType); HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor(responseType, this.getMessageConverters(), this.logger); return this.execute(url, HttpMethod.GET, requestCallback, responseExtractor, (Map)uriVariables); } public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException { RequestCallback requestCallback = this.acceptHeaderRequestCallback(responseType); HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor(responseType, this.getMessageConverters(), this.logger); return this.execute(url, HttpMethod.GET, requestCallback, responseExtractor); }
三类方法调用对应的execute()方法
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Object... uriVariables) throws RestClientException { URI expanded = this.getUriTemplateHandler().expand(url, uriVariables); return this.doExecute(expanded, method, requestCallback, responseExtractor); } public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Map<String, ?> uriVariables) throws RestClientException { URI expanded = this.getUriTemplateHandler().expand(url, uriVariables); return this.doExecute(expanded, method, requestCallback, responseExtractor); } public <T> T execute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor) throws RestClientException { return this.doExecute(url, method, requestCallback, responseExtractor); }
execute()方法最终都调用的是doExecute(url, method, requestCallback, responseExtractor);
所以getForEntity和getForObject的区别就是responseExtractor用的是不同的实现