https://blog.csdn.net/Drug_/article/details/135111675
这一篇的升级版
还是先配置文件
package com.init.config;import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
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.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;/*** restTemplate配置类* @author Json*/@Configuration
public class RestTemplateConfig {@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory factory){return new RestTemplate(factory);}@Beanpublic ClientHttpRequestFactory simpleClientHttpRequestFactory(){HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();factory.setConnectTimeout(15000);factory.setReadTimeout(5000);return factory;}public static HttpComponentsClientHttpRequestFactory generateHttpRequestFactory()throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException{TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());HttpClientBuilder httpClientBuilder = HttpClients.custom();httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);CloseableHttpClient httpClient = httpClientBuilder.build();HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();factory.setHttpClient(httpClient);return factory;}
}
工具类
package com.init.utils;import com.alibaba.fastjson.JSONObject;
import com.kuaijing.init.config.RestTemplateConfig;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;import java.nio.charset.StandardCharsets;
import java.util.List;/****/
public class HttpUtil {/*** 调用第三方服务器接口 能用于上传 附件** @param url 路由* @param jsonObject 参数* @return 响应体里的数据*/public static JSONObject doPostFormData(String url, MultiValueMap<String, Object> jsonObject) throws Exception {RestTemplate restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());//此处加编码格式转换restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);// httpHeaders.setContentType(org.springframework.http.MediaType.APPLICATION_JSON_UTF8);// httpHeaders.put("Authorization",stringList);HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders);//访问第三方服务器ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);return JSONObject.parseObject(exchange.getBody(), JSONObject.class);}/*** 调用第三方服务器接口 能用于上传 附件** @param url 路由* @param jsonObject 参数* @param httpHeaders 请求头* @return 响应体里的数据*/public static JSONObject doPostFormData(String url, MultiValueMap<String, Object> jsonObject, HttpHeaders httpHeaders) throws Exception {RestTemplate restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());//此处加编码格式转换restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders);//访问第三方服务器ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);return JSONObject.parseObject(exchange.getBody(), JSONObject.class);}/*** 调用第三方服务器接口 能用于上传 附件** @param url 路由* @param jsonObject 参数* @return 返回相应所有参数*/public static ResponseEntity<String> doPostFormDataResponse(String url, MultiValueMap<String, Object> jsonObject) throws Exception {RestTemplate restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());//此处加编码格式转换restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);// httpHeaders.setContentType(org.springframework.http.MediaType.APPLICATION_JSON_UTF8);// httpHeaders.put("Authorization",stringList);HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders);//访问第三方服务器return restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);}/*** 调用第三方服务器接口 没有附件上传的那种** @param url 路由* @param jsonObject 参数* @param stringList 头部参数* @return*/public static JSONObject doPostJson(String url, JSONObject jsonObject, List<String> stringList) throws Exception {RestTemplate restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());//此处加编码格式转换restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setContentType(org.springframework.http.MediaType.APPLICATION_JSON_UTF8);httpHeaders.put("Authorization", stringList);HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders);//访问第三方服务器ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);return JSONObject.parseObject(exchange.getBody(), JSONObject.class);}public static JSONObject doPostJson(String url, JSONObject jsonObject) throws Exception {RestTemplate restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());//此处加编码格式转换restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setContentType(org.springframework.http.MediaType.APPLICATION_JSON_UTF8);//httpHeaders.put("Authorization",stringList);HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders);//访问第三方服务器ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);return JSONObject.parseObject(exchange.getBody(), JSONObject.class);}}