配置RestTemplate用于get请求携带multipart/form-data数据
原生的RestTemplate在发送get请求时,无法携带body数据,但是有时候咱们的业务场景需要这样做,所以我们可以对RestTemplate重新配置达到该目的。
1 package com.syx.config; 2 3 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; 4 import org.apache.http.client.methods.HttpUriRequest; 5 import org.apache.http.config.Registry; 6 import org.apache.http.config.RegistryBuilder; 7 import org.apache.http.conn.socket.ConnectionSocketFactory; 8 import org.apache.http.conn.socket.PlainConnectionSocketFactory; 9 import org.apache.http.impl.client.HttpClientBuilder; 10 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 11 import org.springframework.context.annotation.Bean; 12 import org.springframework.context.annotation.Configuration; 13 import org.springframework.http.HttpMethod; 14 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 15 import org.springframework.web.client.RestTemplate; 16 17 import java.net.URI; 18 19 /** 20 * @author:whh 21 * @date: 2022-11-07 20:42 22 * <p></p> 23 */ 24 @Configuration 25 public class RestTemplateConfig { 26 27 @Bean 28 public RestTemplate restTemplate(){ 29 RestTemplate restTemplate = new RestTemplate(); 30 HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory() { 31 @Override 32 protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) { 33 if (httpMethod == HttpMethod.GET) { 34 return new HttpGetRequestWithEntity(uri); 35 } 36 return super.createHttpUriRequest(httpMethod, uri); 37 } 38 }; 39 40 /** 41 * 使用客户端类API调用远程交易时必须设置相应的超时时间,如果远程交易有问题如长时间未响应,若未设置超时时间,则会耗尽客户端所在服务器的资源 42 */ 43 // 连接超时 44 requestFactory.setConnectTimeout(5000); 45 //读超时 46 requestFactory.setReadTimeout(10000); 47 //连接池获取连接超时时间 48 requestFactory.setConnectionRequestTimeout(5000); 49 50 HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); 51 //Httpclient连接池的方式,同时支持netty,okHttp以及其他http框架 52 PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); 53 // 最大连接数 54 connectionManager.setMaxTotal(200); 55 // 同路由并发数 56 connectionManager.setDefaultMaxPerRoute(50); 57 //配置连接池 58 httpClientBuilder.setConnectionManager(connectionManager); 59 60 // 最大连接数 61 connectionManager.setMaxTotal(500); 62 // 同路由并发数(每个主机的并发) 63 connectionManager.setDefaultMaxPerRoute(100); 64 httpClientBuilder.setConnectionManager(connectionManager); 65 requestFactory.setHttpClient(httpClientBuilder.build()); 66 restTemplate.setRequestFactory(requestFactory); 67 return restTemplate; 68 } 69 70 private static final class HttpGetRequestWithEntity extends HttpEntityEnclosingRequestBase { 71 public HttpGetRequestWithEntity(final URI uri) { 72 super.setURI(uri); 73 } 74 @Override 75 public String getMethod() { 76 return HttpMethod.GET.name(); 77 78 } 79 } 80 }
重写一下HttpComponentsClientHttpRequestFactory 的createHttpUriRequest方法在请求方式为get的时候使用我们自定义的HttpGetRequestWithEntity 类
1 @Test 2 public void func2() { 3 HttpHeaders headers = new HttpHeaders(); 4 headers.setContentType(MediaType.MULTIPART_FORM_DATA); 5 MultiValueMap<String, String> form = new LinkedMultiValueMap<>(); 6 form.add("id", "1"); 7 form.add("name", "张三"); 8 form.add("sex","男"); 9 String uri = "http://localhost:8888/user/test"; 10 HttpEntity< MultiValueMap<String, String>> entity = new HttpEntity<>(form,headers); 11 ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class); 12 System.out.println(response.getBody()); 13 14 }
服务端代码:
1 @RequestMapping(value = "/test",method = RequestMethod.GET) 2 public User query(User user){ 3 return user; 4 }
标签:http,springboot,RestTemplate,new,用法,org,apache,import From: https://www.cnblogs.com/DPC27149/p/17213293.html