一、简介
RestTemplate 在JDK HttpURLConnection、Apache HttpComponents、OkHttp等基础上,封装了更高级别的API,默认依赖JDK HttpURLConnection,连接方式默认长连接。
二、使用
2.1、引入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
2.2、创建RestTemplate
2.2.1、配置参数
http:
maxTotal: 100 #最大连接数
defaultMaxPerRoute: 20 #并发数
connectTimeout: 1000 #创建连接的最长时间
connectionRequestTimeout: 500 #从连接池中获取到连接的最长时间
socketTimeout: 10000 #数据传输的最长时间
staleConnectionCheckEnabled: true #提交请求前测试连接是否可用
validateAfterInactivity: 3000000 #可用空闲连接过期时间,重用空闲连接时会先检查是否空闲时间超过这个时间,如果超过,释放socket重新建立
2.2.2、建立Bean注入 IOC
import org.apache.http.Header;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class RestTemplateConfig {
@Value("${http.maxTotal}")
private Integer maxTotal;
@Value("${http.defaultMaxPerRoute}")
private Integer defaultMaxPerRoute;
@Value("${http.connectTimeout}")
private Integer connectTimeout;
@Value("${http.connectionRequestTimeout}")
private Integer connectionRequestTimeout;
@Value("${http.socketTimeout}")
private Integer socketTimeout;
@Value("${http.staleConnectionCheckEnabled}")
private boolean staleConnectionCheckEnabled;
@Value("${http.validateAfterInactivity}")
private Integer validateAfterInactivity;
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(httpRequestFactory());
}
/**
* 使用 Apache HttpClient 作为底层客户端
* 效率: OkHttp > Apache HttpClient > JDK HttpURLConnection
*/
@Bean
public ClientHttpRequestFactory httpRequestFactory() {
return new HttpComponentsClientHttpRequestFactory(httpClient());
}
@Bean
public HttpClient httpClient() {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(maxTotal); // 最大连接数
connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute); // 单个路由最大连接数
connectionManager.setValidateAfterInactivity(validateAfterInactivity); // 最大空间时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout) // 服务器返回数据(response)的时间,超过抛出read timeout
.setConnectTimeout(connectTimeout) // (握手成功)的时间,超出抛出connect timeout
.setStaleConnectionCheckEnabled(staleConnectionCheckEnabled) // 提交前检测是否可用
.setConnectionRequestTimeout(connectionRequestTimeout)// 从连接池中获取连接的超时时间,超时间未拿到可用连接,会抛出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
.build();
// headers
List<Header> headers = new ArrayList<>();
headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36"));
headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate"));
headers.add(new BasicHeader("Accept-Language", "zh-CN"));
headers.add(new BasicHeader("Connection", "Keep-Alive"));
headers.add(new BasicHeader("Content-type", "application/json;charset=UTF-8"));
return HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(connectionManager)
.setDefaultHeaders(headers)
// 保持长连接配置,需要在头添加Keep-Alive
.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
// 重试次数,默认是3次,没有开启
.setRetryHandler(new DefaultHttpRequestRetryHandler(2, true))
.build();
}
}
2.3、API 使用
2.3.1、GET请求
public <T> T getForObject(...):返回值是HTTP协议的响应体内容。
public <T> ResponseEntity<T> getForEntity(...):返回的是ResponseEntity,ResponseEntity包含响应体、HTTP状态码、contentType、contentLength、Header等信息。
Map<String,Object> map = new HashMap<>();
map.put("name","aaa");
map.put("age",12);
// 表单提交传参
ResponseEntity<R> responseEntity = restTemplate.getForEntity(url,R.class,map);
2.3.2、POST请求
public URI postForLocation(...):用POST创建一个新资源,返回UTI对象,可从响应中返回Location报头。
public <T> T postForObject(...):返回HTTP协议的响应体body内容。
public <T> ResponseEntity<T> postForEntity(...):返回ResponseEntity,ResponseEntity包含响应体、HTTP状态码、contentType、contentLength、Header等信息。
RestTemplate restTemplate = new RestTemplate();
String url = "http://172.18.20.200:8080/cms/work/queryWorks";
JSONObject jsonObject = new JSONObject();
jsonObject.set("state","01");
jsonObject.set("title","aaaaa");
// Json 传参
ResponseEntity<R> responseEntity = restTemplate.postForEntity(url,jsonObject,R.class);
System.out.println("---ret-getBody: " +responseEntity.getBody());
System.out.println("---ret-getBody-getCode: " +responseEntity.getBody().getCode());
System.out.println("---ret-getBody-getData: " +responseEntity.getBody().getData());
# POST以表单方式提交
//请求地址
String url2 = "......";
//设置请求头, x-www-form-urlencoded格式的数据
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//提交参数设置
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("name","aaaaaa");
//组装请求体
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, httpHeaders);
//发送post请求并打印结果 以String类型接收响应结果JSON字符串
String s = restTemplate.postForObject(url2, request, String.class);
System.out.println(s);
2.3.3、PUT、DELETE请求
PUT请求
一般用来新增或修改资源,没返回值。
String url = "......";
User user = new User();
user.setName("鲁大师");
restTemplate.put(url,user);
DELETE请求
没返回值,与PUT请求一样。
2.3.4、EXECUTE
RestTemplate restTemplate = new RestTemplate();
//请求地址
String url = "http://172.18.20.200:8080/cms/work/queryWorks";
User user = new User();
user.setName("aaa");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<User> userHttpEntity = new HttpEntity<User>(user, httpHeaders);
ResponseEntity<Object> execute = restTemplate.execute(url, HttpMethod.POST, restTemplate.httpEntityCallback(userHttpEntity), restTemplate.responseEntityExtractor(Object.class));
System.out.println(execute);
2.3.5、上传、下载文件
上传文件
//需要上传的文件
String filePath = "/C:/cms/aaa.jpg";
//请求地址
String url = "http://localhost:8080/rest/file/test/post/upload";
// 请求头设置,multipart/form-data格式的数据
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//提交参数设置
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
param.add("multipartFile", new FileSystemResource(new File(filePath)));
param.add("userCode", "anightmonarch");
param.add("userName", "一宿君");
// 组装请求体
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(param, headers);
//发起请求
ResponseBean responseBean = restTemplate.postForObject(url, request, ResponseBean.class);
System.out.println(responseBean);
下载文件
//需要下载的文件
String fileName = "121dfga0ab3ba.jpg";
//用户信息
String userId = "123456789";
//请求地址
String url = "......";
//提交参数设置
Map<String,Object> paramMap = new HashMap<String,Object>();
paramMap.put("fileName",fileName);
paramMap.put("userId",userId);
//发起请求(响应内容为字节文件)
ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class, paramMap);
System.out.println("HTTP 响应状态:" + response.getStatusCode());
//下载文件保存路径
File savePath = new File("C:\\cms\\download\\img\\");
if (!savePath.isDirectory()){
savePath.mkdirs();
}
Path path = Paths.get(savePath.getPath() + File.separator + fileName);
Files.write(path, Objects.requireNonNull(response.getBody(),"下载文件失败!"));