【spring boot】 使用 RestTemplate
生活随笔
收集整理的這篇文章主要介紹了
【spring boot】 使用 RestTemplate
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這里寫目錄標題
- 前言
- 準備
- 引入 lib
- 創建 RestTemplate Bean
- 認識一下RestTemplate
- getForObject 示例
- 簡單調用
- 參數拼接
- postForEntity 示例
- 構造 request Object
- 在 URL 中傳參
- postForObject 示例
- 構造 request Object
- 在 URL 中傳參
- (POST)Content-Type: application/x-www-form-urlencoded
- (POST)Content-Type: application/json
- (POST)Content-Type: application/json
- postForEntity 與 postForObject 的區別
前言
- spring-boot 2.0.0.RELEASE
- spring-web 5.0.4.RELEASE
- maven 3.5.0
- Eclipse Version: 2019-09 R (4.13.0)
- 使用 RestTemplate 調用 restful 接口
- 官方說明在這里和這里
準備
引入 lib
RestTemplate 類的完整路徑為 : org.springframework.web.client.RestTemplate 。
RestTemplate 類在 lib : spring-web 中。
springboot項目引入 lib:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency>創建 RestTemplate Bean
@Configuration public class RestTemplateConfig {@Beanpublic RestTemplate getRestTemplate() {return new RestTemplate();} }說明:
- 也可以不創建Bean。不創建Bean時,需要每次new RestTemplate()。
- 如果既不創建Bean,也不每次new RestTemplate(),則會遇到錯誤No qualifying bean of type 'org.springframework.web.client.RestTemplate'
認識一下RestTemplate
常用方法,getForEntity和getForObject方法、postForEntity和postForObject方法。ForEntity方法和ForObject方法的差別在于返回值。ResponseEntity類型的返回值可以獲取到完整的Response信息,比如header、http status。部分相關方法定義:
- public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException
- public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException
- public <T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException
- public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException
exchange方法更偏向于自己處理RequestEntity的情況。
getForObject 示例
簡單調用
String url = "https://www.so.com/s?q=abc"; // 將url返回的內容,存入變量result中 String result = this.restTemplate.getForObject(url, String.class);參數拼接
String url = "https://www.so.com/s?q={keyword}"; Map<String, String> paramMap = new HashMap<>(); paramMap.put("keyword", "abc"); // 將url返回的內容,存入變量result中 String result = this.restTemplate.getForObject(url, String.class, paramMap);postForEntity 示例
構造 request Object
@Service public class DataSendService {@Autowiredprivate RestTemplate restTemplate;public void sendData() {String url = "http://localhost:8080/app/data-accept";HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);MultiValueMap<String, String> paramMap= new LinkedMultiValueMap<>();paramMap.add("name", "zhangsan");HttpEntity<MultiValueMap<String, String>> entity = null;entity = new HttpEntity<>(paramMap, headers);ResponseEntity<String> exchange = this.restTemplate.postForEntity(url, entity, String.class);String result = exchange.getBody();System.out.println(result);} }在 URL 中傳參
@Service public class DataSendService {@Autowiredprivate RestTemplate restTemplate;public void sendData() {String url = "http://localhost:8080/app/data-accept?name={name}";ResponseEntity<String> exchange = this.restTemplate.postForEntity(url, null, String.class, "zhangsan");System.out.println(exchange.getBody());} }postForObject 示例
構造 request Object
@Service public class DataSendService {@Autowiredprivate RestTemplate restTemplate;public void sendData() {String url = "http://localhost:8080/app/data-accept";HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);MultiValueMap<String, String> paramMap= new LinkedMultiValueMap<>();paramMap.add("name", "zhangsan");HttpEntity<MultiValueMap<String, String>> entity = null;entity = new HttpEntity<>(paramMap, headers);String result = this.restTemplate.postForObject(url, entity, String.class);System.out.println(result);} }在 URL 中傳參
@Service public class DataSendService {@Autowiredprivate RestTemplate restTemplate;public void sendData() {String url = "http://localhost:8080/app/data-accept?name={name}";String result = this.restTemplate.postForObject(url, null, String.class, "zhangsan");System.out.println(result);} }(POST)Content-Type: application/x-www-form-urlencoded
@Service public class DataSendService {@Autowiredprivate RestTemplate restTemplate;public void sendData() {String url = "http://localhost:8080/app/data-accept";HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);MultiValueMap<String, String> paramMap= new LinkedMultiValueMap<>();paramMap.add("name", "zhangsan");HttpEntity<MultiValueMap<String, String>> entity = null;entity = new HttpEntity<>(paramMap, headers);String result = this.restTemplate.postForObject(url, entity, String.class);System.out.println(result);} }(POST)Content-Type: application/json
@Service public class DataSendService {@Autowiredprivate RestTemplate restTemplate;public void sendData() {String url = "http://localhost:8080/app/data-accept";HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);JSONObject jsonBody = new JSONObject();jsonBody.put("name", "zhangsan");jsonBody.put("age", 18);HttpEntity<JSONObject> entity = null;entity = new HttpEntity<>(jsonBody , headers);String result = this.restTemplate.postForObject(url, entity, String.class);System.out.println(result);} }(POST)Content-Type: application/json
@Service public class DataSendService {@Autowiredprivate RestTemplate restTemplate;public void sendData() {String url = "http://localhost:8080/app/data-accept";HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.TEXT_PLAIN);String message = "this is a text";HttpEntity<String > entity = null;entity = new HttpEntity<>(message , headers);String result = this.restTemplate.postForObject(url, entity, String.class);System.out.println(result);} }postForEntity 與 postForObject 的區別
參數一樣,功能一樣,返回值類型不一樣。
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType,Object... uriVariables) throws RestClientException {... } public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType,Map<String, ?> uriVariables) throws RestClientException {... } public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType)throws RestClientException {... } public <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request,Class<T> responseType, Object... uriVariables) throws RestClientException {... } public <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request,Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {... } @Override public <T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType)throws RestClientException {... }總結
以上是生活随笔為你收集整理的【spring boot】 使用 RestTemplate的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国手机银行怎样开通和取消?
- 下一篇: Appium 与 Chromedrive