| New file | 
|  |  |  | 
|---|
|  |  |  | package com.dy.pipIrrSell.config; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.context.annotation.Bean; | 
|---|
|  |  |  | import org.springframework.context.annotation.Configuration; | 
|---|
|  |  |  | import org.springframework.http.converter.HttpMessageConverter; | 
|---|
|  |  |  | import org.springframework.http.converter.StringHttpMessageConverter; | 
|---|
|  |  |  | import org.springframework.web.client.RestTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.nio.charset.StandardCharsets; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author ZhuBaoMin | 
|---|
|  |  |  | * @date 2024-03-06 11:43 | 
|---|
|  |  |  | * @LastEditTime 2024-03-06 11:43 | 
|---|
|  |  |  | * @Description | 
|---|
|  |  |  | */ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Configuration | 
|---|
|  |  |  | public class RestTemplateConfig { | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RestTemplateWechatCertConfig restTemplateWechatCertConfig; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Bean() | 
|---|
|  |  |  | public RestTemplate restTemplate() throws Exception { | 
|---|
|  |  |  | RestTemplate restTemplate = new RestTemplate(restTemplateWechatCertConfig.wechatHttpRequestFactory()); | 
|---|
|  |  |  | // 添加拦截器 | 
|---|
|  |  |  | //List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); | 
|---|
|  |  |  | //RestTemplateWechatCertConfig.MyRequestInterceptor myRequestInterceptor = new RestTemplateWechatCertConfig.MyRequestInterceptor(); | 
|---|
|  |  |  | //interceptors.add(myRequestInterceptor); | 
|---|
|  |  |  | //restTemplate.setInterceptors(interceptors); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 中文乱码,主要是 StringHttpMessageConverter的默认编码为ISO导致的 | 
|---|
|  |  |  | List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters(); | 
|---|
|  |  |  | for (HttpMessageConverter converter : list) { | 
|---|
|  |  |  | if (converter instanceof StringHttpMessageConverter) { | 
|---|
|  |  |  | ((StringHttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return restTemplate; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //简单RestTemplate实例 | 
|---|
|  |  |  | @Bean | 
|---|
|  |  |  | public RestTemplate simpleRestTemplate() { | 
|---|
|  |  |  | return new RestTemplate(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|