回到首頁☞
http請求是java web程式設計的核心功能,目前主流的方式三類java原生的,HttpClient和Spring的restTemplate。
對于一些老舊的jalor的系統會發現,換一撥人就會引入一個工具類,三種都用到了。
目前主流的java web架構就是spring全家桶,企業的二次封裝後底層還是spring core,是以玩轉restTemplate即可。
1、概述
spring架構提供的RestTemplate類可用于在應用中調用rest服務,它簡化了與http服務的通信方式,統一了RESTful的标準,封裝了http連結, 我們隻需要傳入url及傳回值類型即可。相較于之前常用的HttpClient,RestTemplate是一種更優雅的調用RESTful服務的方式。
在Spring應用程式中通路第三方REST服務與使用Spring RestTemplate類有關。RestTemplate類的設計原則與許多其他Spring *模闆類(例如JdbcTemplate、JmsTemplate)相同,為執行複雜任務提供了一種具有預設行為的簡化方法。
RestTemplate預設依賴JDK提供http連接配接的能力(HttpURLConnection),如果有需要的話也可以通過setRequestFactory方法替換為例如 Apache HttpComponents、Netty或OkHttp等其它HTTP library。
考慮到RestTemplate類是為調用REST服務而設計的,是以它的主要方法與REST的基礎緊密相連就不足為奇了,後者是HTTP協定的方法:HEAD、GET、POST、PUT、DELETE和OPTIONS。例如,RestTemplate類具有headForHeaders()、getForObject()、postForObject()、put()和delete()等方法。
2、修改下pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wht.springboot.demo</groupId>
<artifactId>com.wht.springboot.demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>mySpringbootDemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8
</project.reporting.outputEncoding>
<mybatis-spring-boot.version>1.2.0</mybatis-spring-boot.version>
<mysql-connector.version>5.1.39</mysql-connector.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector.version}</version>
</dependency>
<!--線程池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 熱部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、增加RestTemplateUtils
RestTemplate 在springboot中可以啟動時增加bean,也可以采用RestTemplateConfig,這裡暫時使用
RestTemplateUtils,因為這樣傳統的springmvc也可以使用。
package com.wht.springboot.demo.util;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
/**
* RestTemplate 遠端調用工具類
*
* @author JDIT
* @createDate 2020-05-05
*
*/
public class RestTemplateUtils {
private static final RestTemplate restTemplate = new RestTemplate();
// ----------------------------------GET-------------------------------------------------------
/**
* GET請求調用方式
*
* @param url 請求URL
* @param responseType 傳回對象類型
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> get(String url, Class<T> responseType) {
return restTemplate.getForEntity(url, responseType);
}
/**
* GET請求調用方式
*
* @param url 請求URL
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> get(String url, Class<T> responseType, Object... uriVariables) {
return restTemplate.getForEntity(url, responseType, uriVariables);
}
/**
* GET請求調用方式
*
* @param url 請求URL
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> get(String url, Class<T> responseType, Map<String, ?> uriVariables) {
return restTemplate.getForEntity(url, responseType, uriVariables);
}
/**
* 帶請求頭的GET請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> get(String url, Map<String, String> headers, Class<T> responseType, Object... uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return get(url, httpHeaders, responseType, uriVariables);
}
/**
* 帶請求頭的GET請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> get(String url, HttpHeaders headers, Class<T> responseType, Object... uriVariables) {
HttpEntity<?> requestEntity = new HttpEntity<>(headers);
return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables);
}
/**
* 帶請求頭的GET請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> get(String url, Map<String, String> headers, Class<T> responseType, Map<String, ?> uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return get(url, httpHeaders, responseType, uriVariables);
}
/**
* 帶請求頭的GET請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> get(String url, HttpHeaders headers, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<?> requestEntity = new HttpEntity<>(headers);
return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables);
}
// ----------------------------------POST-------------------------------------------------------
/**
* POST請求調用方式
*
* @param url 請求URL
* @param responseType 傳回對象類型
* @return
*/
public static <T> ResponseEntity<T> post(String url, Class<T> responseType) {
return restTemplate.postForEntity(url, HttpEntity.EMPTY, responseType);
}
/**
* POST請求調用方式
*
* @param url 請求URL
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType) {
return restTemplate.postForEntity(url, requestBody, responseType);
}
/**
* POST請求調用方式
*
* @param url 請求URL
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType, Object... uriVariables) {
return restTemplate.postForEntity(url, requestBody, responseType, uriVariables);
}
/**
* POST請求調用方式
*
* @param url 請求URL
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
return restTemplate.postForEntity(url, requestBody, responseType, uriVariables);
}
/**
* 帶請求頭的POST請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> post(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return post(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 帶請求頭的POST請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> post(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return post(url, requestEntity, responseType, uriVariables);
}
/**
* 帶請求頭的POST請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> post(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return post(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 帶請求頭的POST請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> post(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return post(url, requestEntity, responseType, uriVariables);
}
/**
* 自定義請求頭和請求體的POST請求調用方式
*
* @param url 請求URL
* @param requestEntity 請求頭和請求體封裝對象
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> post(String url, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {
return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType, uriVariables);
}
/**
* 自定義請求頭和請求體的POST請求調用方式
*
* @param url 請求URL
* @param requestEntity 請求頭和請求體封裝對象
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> post(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {
return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType, uriVariables);
}
// ----------------------------------PUT-------------------------------------------------------
/**
* PUT請求調用方式
*
* @param url 請求URL
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> put(String url, Class<T> responseType, Object... uriVariables) {
return put(url, HttpEntity.EMPTY, responseType, uriVariables);
}
/**
* PUT請求調用方式
*
* @param url 請求URL
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> put(String url, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody);
return put(url, requestEntity, responseType, uriVariables);
}
/**
* PUT請求調用方式
*
* @param url 請求URL
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> put(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody);
return put(url, requestEntity, responseType, uriVariables);
}
/**
* 帶請求頭的PUT請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> put(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return put(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 帶請求頭的PUT請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return put(url, requestEntity, responseType, uriVariables);
}
/**
* 帶請求頭的PUT請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> put(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return put(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 帶請求頭的PUT請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return put(url, requestEntity, responseType, uriVariables);
}
/**
* 自定義請求頭和請求體的PUT請求調用方式
*
* @param url 請求URL
* @param requestEntity 請求頭和請求體封裝對象
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> put(String url, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {
return restTemplate.exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables);
}
/**
* 自定義請求頭和請求體的PUT請求調用方式
*
* @param url 請求URL
* @param requestEntity 請求頭和請求體封裝對象
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> put(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {
return restTemplate.exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables);
}
// ----------------------------------DELETE-------------------------------------------------------
/**
* DELETE請求調用方式
*
* @param url 請求URL
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, Class<T> responseType, Object... uriVariables) {
return delete(url, HttpEntity.EMPTY, responseType, uriVariables);
}
/**
* DELETE請求調用方式
*
* @param url 請求URL
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, Class<T> responseType, Map<String, ?> uriVariables) {
return delete(url, HttpEntity.EMPTY, responseType, uriVariables);
}
/**
* DELETE請求調用方式
*
* @param url 請求URL
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody);
return delete(url, requestEntity, responseType, uriVariables);
}
/**
* DELETE請求調用方式
*
* @param url 請求URL
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody);
return delete(url, requestEntity, responseType, uriVariables);
}
/**
* 帶請求頭的DELETE請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Class<T> responseType, Object... uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return delete(url, httpHeaders, responseType, uriVariables);
}
/**
* 帶請求頭的DELETE請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);
return delete(url, requestEntity, responseType, uriVariables);
}
/**
* 帶請求頭的DELETE請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Class<T> responseType, Map<String, ?> uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return delete(url, httpHeaders, responseType, uriVariables);
}
/**
* 帶請求頭的DELETE請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);
return delete(url, requestEntity, responseType, uriVariables);
}
/**
* 帶請求頭的DELETE請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return delete(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 帶請求頭的DELETE請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return delete(url, requestEntity, responseType, uriVariables);
}
/**
* 帶請求頭的DELETE請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return delete(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 帶請求頭的DELETE請求調用方式
*
* @param url 請求URL
* @param headers 請求頭參數
* @param requestBody 請求參數體
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return delete(url, requestEntity, responseType, uriVariables);
}
/**
* 自定義請求頭和請求體的DELETE請求調用方式
*
* @param url 請求URL
* @param requestEntity 請求頭和請求體封裝對象
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {
return restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables);
}
/**
* 自定義請求頭和請求體的DELETE請求調用方式
*
* @param url 請求URL
* @param requestEntity 請求頭和請求體封裝對象
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> delete(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {
return restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables);
}
// ----------------------------------通用方法-------------------------------------------------------
/**
* 通用調用方式
*
* @param url 請求URL
* @param method 請求方法類型
* @param requestEntity 請求頭和請求體封裝對象
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,按順序依次對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {
return restTemplate.exchange(url, method, requestEntity, responseType, uriVariables);
}
/**
* 通用調用方式
*
* @param url 請求URL
* @param method 請求方法類型
* @param requestEntity 請求頭和請求體封裝對象
* @param responseType 傳回對象類型
* @param uriVariables URL中的變量,與Map中的key對應
* @return ResponseEntity 響應對象封裝類
*/
public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {
return restTemplate.exchange(url, method, requestEntity, responseType, uriVariables);
}
/**
* 擷取RestTemplate執行個體對象,可自由調用其方法
*
* @return RestTemplate執行個體對象
*/
public static RestTemplate getRestTemplate() {
return restTemplate;
}
}
4、增加test類-RestTest
import com.alibaba.fastjson.JSONObject;
import com.wht.springboot.demo.util.RestTemplateUtils;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author JDIT
*/
public class RestTest {
/**
*
* get path路徑參數
* 頭在真實項目是必須的基本都要涉及SDK安全認證
*/
@Test
public void test() {
String url = "http://localhost:9090/test/sayHello/{id}/{name}";
Map<String, String> headers = new HashMap<>();
headers.put("token", "12333");
ResponseEntity<String> entity = RestTemplateUtils.get(url, headers,String.class, 123,"Jack");
System.out.println(entity.getStatusCode());
System.out.println(entity.getBody());
}
}
5、進行調試
- 先啟動springboot工程
- 運作test
6、Get請求之路徑參數
6.1、增加測試接口
@RequestMapping(value = "sayHello/{id}/{name}",method=RequestMethod.GET)
public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name) {
return String.format("Hello World %s,your id is %d",name,id );
}
6.2、增加Test
/**
*
* get path路徑參數
* 頭在真實項目是必須的基本都要涉及SDK安全認證
*/
@Test
public void test() {
String url = "http://localhost:9090/test/sayHello/{id}/{name}";
Map<String, String> headers = new HashMap<>();
headers.put("token", "12333");
ResponseEntity<String> entity = RestTemplateUtils.get(url, headers,String.class, 123,"Jack");
System.out.println(entity.getStatusCode());
System.out.println(entity.getBody());
}
7、Get請求之Query傳參
7.1、增加測試接口
@RequestMapping(value ="sayHelloRequestParam",method= RequestMethod.GET)
public String sayHelloRequestParam(@RequestParam("name") String name) {
return String.format("Hello World %s",name );
}
7.2、增加Test
/**
* 測試帶請求頭參數Headers的GET請求,POST類似
*/
@Test
public void testHeaders() {
String url = "http://localhost:9090/test/sayHelloRequestParam";
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("name", null).queryParam("id",123);
System.out.println(builder.toUriString());
Map<String, String> headers = new HashMap<>();
headers.put("token", "12333");
ResponseEntity<String> entity = RestTemplateUtils.get(builder.toUriString(), headers, String.class);
System.out.println(entity.getStatusCode());
System.out.println(entity.getBody());
}
回到首頁☞