SpringMVC - 设置UTF-8编码

参考

      一下这些都是直接从园友直接搬过来的

    • sxhjhf - springMVC @response 中文乱码解决 
      修改Response的编码, 这是基于xml配置文件的方法.
    • spring-mvc-utf-8-encoding
      这是基于xml配置的, 关于输入的编码的问题.
    • 全局修改输出为UTF-8编码

      1. xml 方法
        sxhjhf - springMVC @response 中文乱码解决
        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
            xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xsi:schemaLocation="
               http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
               http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
            <!-- utf-8编码 -->
            <mvc:annotation-driven>
                <mvc:message-converters register-defaults="true">
                    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                        <constructor-arg value="UTF-8" />
                    </bean>
                </mvc:message-converters>
            </mvc:annotation-driven>
        </beans>
        
        作者:xiaofudeng
        链接:https://www.jianshu.com/p/f6f28576a564
        來源:简书
        简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
        1. annotation方法
          由方法1, 我们可以看到一个起关键作用的类org.springframework.http.converter.StringHttpMessageConverter. 打开源码可以发现:
          public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
                  // 省略 ....
                  public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
                  private final List<Charset> availableCharsets;
                  private boolean writeAcceptCharset = true;
                  
                  public StringHttpMessageConverter(Charset defaultCharset) {
                      super(defaultCharset, MediaType.TEXT_PLAIN, MediaType.ALL);
                      this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());
                  }
                  // 省略 ....
              }
          
          作者:xiaofudeng
          链接:https://www.jianshu.com/p/f6f28576a564
          來源:简书
          简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
          可以看到, 默认编码已经被设定为了 Charset.forName("ISO-8859-1"). 所以我们需要想办法替换掉这个默认编码. 方法1是通过 xml加载了参数为 UTF-8StringHttpMessageConverter.
          回到使用注解配置的Java代码中:
          因为配置SpringMVC的类, 需要继承于 WebMvcConfigurerAdapter, 我们可以看看里面有没有相关方法可用.


          作者:xiaofudeng
          链接:https://www.jianshu.com/p/f6f28576a564
          來源:简书
          简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
          @Configuration
          @EnableWebMvc
          @ComponentScan("me.xiaofud.spring101.spittr.web")
          public class WebConfig extends WebMvcConfigurerAdapter {
              @Override
              public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
                  StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
                  stringHttpMessageConverter.setWriteAcceptCharset(false);
                  converters.add(stringHttpMessageConverter);
              }
          
          作者:xiaofudeng
          链接:https://www.jianshu.com/p/f6f28576a564
          來源:简书
          简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

          测试:

          curl -I -X POST "http://localhost:8080/app/post/add"
          HTTP/1.1 200 OK
          Server: Apache-Coyote/1.1
          Content-Type: text/plain;charset=UTF-8
          Content-Length: 1338
          Date: Sun, 12 Nov 2017 14:17:49 GMT
          
          

          修改读取参数时候的编码

          web.xml中:
          添加一个filter, 注册org.springframework.web.filter.CharacterEncodingFilter.

          <filter>  
              <filter-name>encodingFilter</filter-name>  
              <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
              <init-param>  
                 <param-name>encoding</param-name>  
                 <param-value>UTF-8</param-value>  
              </init-param>  
              <init-param>  
                 <param-name>forceEncoding</param-name>  
                 <param-value>true</param-value>  
              </init-param>  
          </filter>  
          <filter-mapping>  
              <filter-name>encodingFilter</filter-name>  
              <url-pattern>/*</url-pattern>  
          </filter-mapping>

猜你喜欢

转载自www.cnblogs.com/cheng88/p/10064428.html