在对接微信公众平台过程中,需要调用“获取草稿列表”的接口,其官网地址如下:https://developers.weixin.qq.com/doc/offiaccount/Draft_Box/Get_draft_list.html,在测试过程中发现返回的中文乱码,其表现如下:
响应结果中,title、author、content等字段出现中文乱码问题。
public BaseDataResponse getDraftList(WechatDraftListRequest draftListRequest) {
// 创建响应结果
WechatDraftListResponse resp;
String organizationCode = draftListRequest.getOrganizationCode();
// 校验请求参数
if (StringUtils.isBlank(organizationCode)) {
throw new BusinessException(ResultCode.PARAMS_ERROR);
}
log.info("organizationCode:{} getDraftList requestParam:{}", organizationCode, draftListRequest);
// 根据机构号获取对应的access_token
String accessToken = customerOpenService.queryAccessToken(organizationCode);
String draftUrl = String.format(draftListUrl, accessToken);
// 组装请求参数
Map<String, Object> requestParameters = new HashMap<>();
requestParameters.put(WechatConstants.RESPONSE_COUNT, draftListRequest.getCount());
requestParameters.put(WechatConstants.RESPONSE_OFFSET, draftListRequest.getOffset());
HttpEntity entity = new HttpEntity(requestParameters, null);
ResponseEntity<String> res = restTemplate.exchange(draftUrl, HttpMethod.POST, entity, String.class);
organizationCode, JSONObject.toJSON(res.getHeaders()));
String str = res.getBody();
log.info("organizationCode:{} getDraftList str:{}", organizationCode, str);
JSONObject json = JSONObject.parseObject(str);
// 微信
if (!ObjectUtils.isEmpty(json.getInteger(WechatConstants.ERRCODE))) {
log.error("organizationCode:{} getDraftList wechat response error:{}", organizationCode, str);
return BaseDataResponse.error(json.getInteger(WechatConstants.ERRCODE).toString()
, json.getString(WechatConstants.ERRMSG));
}
resp = JSON.toJavaObject(json, WechatDraftListResponse.class);
return BaseDataResponse.success(resp);
}
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
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.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Value("${ok.http.connect-timeout}")
private Integer connectTimeout;
@Value("${ok.http.read-timeout}")
private Integer readTimeout;
@Value("${ok.http.write-timeout}")
private Integer writeTimeout;
@Value("${ok.http.max-idle-connections}")
private Integer maxIdleConnections;
@Value("${ok.http.keep-alive-duration}")
private Long keepAliveDuration;
/**
* 声明 RestTemplate
*/
@Bean
public RestTemplate httpRestTemplate() {
ClientHttpRequestFactory factory = httpRequestFactory();
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.getMessageConverters().add(new CustomerMappingJackson2HttpMessageConverter());
List<HttpMessageConverter<?>> httpMessageConverters = restTemplate.getMessageConverters();
return restTemplate;
}
public ClientHttpRequestFactory httpRequestFactory() {
return new OkHttp3ClientHttpRequestFactory(okHttpConfigClient());
}
public OkHttpClient okHttpConfigClient() {
return new OkHttpClient().newBuilder()
.connectionPool(pool())
.connectTimeout(connectTimeout, TimeUnit.SECONDS)
.readTimeout(readTimeout, TimeUnit.SECONDS)
.writeTimeout(writeTimeout, TimeUnit.SECONDS)
.hostnameVerifier((hostname, session) -> true)
// 设置代理
// .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)))
// 拦截器
// .addInterceptor()
.build();
}
public ConnectionPool pool() {
return new ConnectionPool(maxIdleConnections, keepAliveDuration, TimeUnit.SECONDS);
}
}
这里引用了okhttp
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.12.2</version>
</dependency>
乱码问题最常见的解决方法就是在请求头中设置Content-Type,于是首先在http请求头中设置了Content-Type,添加如下代码:
添加之后,再次测试发现乱码依然存在,问题没有解决。
通过查询配置了springboot全局解决中文乱码的问题的配置类,代码如下:
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class CharsetConfig extends WebMvcConfigurationSupport {
@Bean
public HttpMessageConverter<String> responseBodyConverter() {
return new StringHttpMessageConverter(
StandardCharsets.UTF_8);
}
@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
converters.add(responseBodyConverter());
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
}
配置之后,重启服务,重新测试,发现依旧乱码,问题仍然没有解决。
1.此时,我在代码中增加了两行日志
重新调试,发现打印结果如下:
发现响应里面的编码格式竟然为空,而Content-Type是text/plain,只能吐槽一句,微信接口是真的坑啊。
2.debug进入了StringHttpMessageConverter类,发现解码的时候走到了StringHttpMessageConverter类的readInternal方法,如图:
3.继续进入getContentTypeCharset方法
发现响应的charset为null,Content-Type是text/plain的情况下,使用的是默认编码StandardCharsets.ISO_8859_1。至此真相大白。
于是在配置RestTemplate的代码,配置类RestTemplateConfig中修改生成RestTemplate对象bean的代码如下:
@Bean
public RestTemplate httpRestTemplate() {
ClientHttpRequestFactory factory = httpRequestFactory();
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.getMessageConverters().add(new CustomerMappingJackson2HttpMessageConverter());
List<HttpMessageConverter<?>> httpMessageConverters = restTemplate.getMessageConverters();
httpMessageConverters.stream().forEach(httpMessageConverter -> {
if (httpMessageConverter instanceof StringHttpMessageConverter) {
StringHttpMessageConverter messageConverter = (StringHttpMessageConverter) httpMessageConverter;
messageConverter.setDefaultCharset(Charset.forName("UTF-8"));
}
});
return restTemplate;
}
其中主要是添加了如下代码:
至此问题解决。
验证结果,发现中文乱码问题得到解决。如图: