评论

收藏

[Java] 将RestTemplate的编码格式改为UTF-8,防止乱码问题

编程语言 编程语言 发布于:2022-03-04 16:45 | 阅读数:331 | 评论:0

这篇文章主要介绍了将RestTemplate的编码格式改为UTF-8,防止乱码问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
目录

  • RestTemplate编码格式改为UTF-8,防止乱码
  • RestTemplate 中文乱码配置

    • 先看说如何解决
    • 再看看为什么会乱码


RestTemplate编码格式改为UTF-8,防止乱码
我是在调用微信的API 的时候发现微信给我返回的用户数据不能够正常显示昵称,昵称都是乱码。
//修改RestTemplate的编码格式为UTF-8
     RestTemplate restTemplate = new RestTemplate();
     List<HttpMessageConverter<?>> httpMessageConverters = restTemplate.getMessageConverters();
     httpMessageConverters.stream().forEach(httpMessageConverter -> {
       if(httpMessageConverter instanceof StringHttpMessageConverter){
         StringHttpMessageConverter messageConverter = (StringHttpMessageConverter) httpMessageConverter;
         messageConverter.setDefaultCharset(Charset.forName("UTF-8"));
       }
       //发送请求
     String jsonStr = restTemplate.getForEntity(url, String.class).getBody();
上面的代码中很简单的写出来了,直接自己分装成一个方法就好。这样就解决了中文的乱码问题了

RestTemplate 中文乱码配置
restTemplate作为spring web client下的一个工具类 对http请求做了一层封装,用起来也更加简洁容易,但最近遇到一个问题就是在发送请求时由于请求中包含中文导致乱码,都变成???????一堆问号,网上很多解决方案,但很多都比较…..

先看说如何解决
@Bean配置方法:
@Bean
  public RestTemplate restTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
    return restTemplate;
  }
applicationContext.xml配置方法:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
     default-autowire="byName" default-lazy-init="true">
  <!--方式一、使用jdk的实现-->
  <bean id="ky.requestFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory">
    <property name="readTimeout" value="10000"/>
    <property name="connectTimeout" value="5000"/>
  </bean>
  <bean id="simpleRestTemplate" class="org.springframework.web.client.RestTemplate">
    <constructor-arg ref="ky.requestFactory"/>
    <property name="messageConverters">
      <list>
        <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
          <property name="supportedMediaTypes">
            <list>
              <value>text/plain;charset=UTF-8</value>
            </list>
          </property>
        </bean>
      </list>
    </property>
  </bean>
</beans>
然后在使用的地方自动注入就好啦~~

再看看为什么会乱码
public RestTemplate() {
    this.messageConverters.add(new ByteArrayHttpMessageConverter());
    this.messageConverters.add(new StringHttpMessageConverter());
    this.messageConverters.add(new ResourceHttpMessageConverter());
    this.messageConverters.add(new SourceHttpMessageConverter<Source>());
    this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());
    if (romePresent) {
      this.messageConverters.add(new AtomFeedHttpMessageConverter());
      this.messageConverters.add(new RssChannelHttpMessageConverter());
    }
    if (jaxb2Present) {
      this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
    }
    if (jackson2Present) {
      this.messageConverters.add(new MappingJackson2HttpMessageConverter());
    }
    else if (jacksonPresent) {
      this.messageConverters.add(new MappingJacksonHttpMessageConverter());
    }
  }
从RestTemplate 构造方法可以看出restTemplate默认的messageConverters有好几个,这次的主角是StringHttpMessageConverter:
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
  public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
  private final Charset defaultCharset;
  private final List<Charset> availableCharsets;
  private boolean writeAcceptCharset = true;
  /**
   * A default constructor that uses {@code "ISO-8859-1"} as the default charset.
   * @see #StringHttpMessageConverter(Charset)
   */
  public StringHttpMessageConverter() {
    this(DEFAULT_CHARSET);
  }
  /**
   * A constructor accepting a default charset to use if the requested content
   * type does not specify one.
   */
  public StringHttpMessageConverter(Charset defaultCharset) {
    super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);
    this.defaultCharset = defaultCharset;
    this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());
  }
  /**
   * Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
   * <p>Default is {@code true}.
   */
  public void setWriteAcceptCharset(boolean writeAcceptCharset) {
    this.writeAcceptCharset = writeAcceptCharset;
  }
  @Override
  public boolean supports(Class<?> clazz) {
    return String.class.equals(clazz);
  }
  @Override
  protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException {
    Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());
    return StreamUtils.copyToString(inputMessage.getBody(), charset);
  }
  @Override
  protected Long getContentLength(String s, MediaType contentType) {
    Charset charset = getContentTypeCharset(contentType);
    try {
      return (long) s.getBytes(charset.name()).length;
    }
    catch (UnsupportedEncodingException ex) {
      // should not occur
      throw new IllegalStateException(ex);
    }
  }
  @Override
  protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException {
    if (this.writeAcceptCharset) {
      outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
    }
    Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
    StreamUtils.copy(s, charset, outputMessage.getBody());
  }
  /**
   * Return the list of supported {@link Charset}.
   * <p>By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses.
   * @return the list of accepted charsets
   */
  protected List<Charset> getAcceptedCharsets() {
    return this.availableCharsets;
  }
  private Charset getContentTypeCharset(MediaType contentType) {
    if (contentType != null && contentType.getCharSet() != null) {
      return contentType.getCharSet();
    }
    else {
      return this.defaultCharset;
    }
  }
}
可以看到StringHttpMessageConverter默认是ISO-8859-1编码(有空可以看下别的MessageConverter其实都是UTF-8的),所以就把它改了就好啦~
以上为个人经验,希望能给大家一个参考,也希望大家多多支持CodeAE代码之家
原文链接:https://blog.csdn.net/myhAini/article/details/106280085

关注下面的标签,发现更多相似文章