zoukankan      html  css  js  c++  java
  • Spring @ResponseBody 返回乱码 的优雅解决办法

    目录(?)[+]

    返回的结果中,中文全部被问号(?)代替的解决办法:

    *-servlet.xml的部分配置如下:
    1. <bean id="utf8Charset" class="java.nio.charset.Charset"  
    2.         factory-method="forName">  
    3.         <constructor-arg value="UTF-8"/>  
    4.     </bean>  
    5.   
    6.     <mvc:annotation-driven>  
    7.         <mvc:message-converters>  
    8.             <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
    9.                 <constructor-arg ref="utf8Charset" />  
    10.             </bean>  
    11.         </mvc:message-converters>  
    12.     </mvc:annotation-driven>  

    思路非常简单:查看StringHttpMessageConverter可知其有两个构造函数,

    1. public StringHttpMessageConverter() {  
    2.     this(DEFAULT_CHARSET);  
    3. }  
    1. public StringHttpMessageConverter(Charset defaultCharset) {  
    2.     super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);  
    3.     this.defaultCharset = defaultCharset;  
    4.     this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());  
    5. }  

    这里采用第二个构造函数向其中注入我们的utf8Charset


    上面配置会覆盖 mvc:annotation-driven 默认配置的StringHttpMessageConverter,其余的MessageConverter应该不会受到影响。

    具体文档描述如下:

    1. Element : message-converters  
    2. Configures one or more HttpMessageConverter types to use for converting @RequestBody method parameters and   
    3.  @ResponseBody method return values. Using this configuration element is optional. HttpMessageConverter   
    4.  registrations provided here will take precedence over HttpMessageConverter types registered by default. Also see   
    5.  the register-defaults attribute if you want to turn off default registrations entirely.  

    <mvc:annotation-driven>会默认配置一些HttpMessageConverter,Spring Reference描述如下:

    1. HttpMessageConverter support for @RequestBody method parameters and @ResponseBody method return values from @RequestMapping or @ExceptionHandler methods.  
    2. This is the complete list of HttpMessageConverters set up by mvc:annotation-driven:  
    3. ByteArrayHttpMessageConverter converts byte arrays.  
    4. StringHttpMessageConverter converts strings.  
    5. ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.  
    6. SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.  
    7. FormHttpMessageConverter converts form data to/from a MultiValueMap<String, String>.  
    8. Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present on the classpath.  
    9. MappingJackson2HttpMessageConverter (or MappingJacksonHttpMessageConverter) converts to/from JSON — added if Jackson 2 (or Jackson) is present on the classpath.  
    10. AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.  
    11. RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.  


    返回的结果中,中文全部是乱码(非问号)的解决办法:

    在web.xml中加入以下Filter:

    1. <filter>  
    2.         <filter-name>encodingFilter</filter-name>  
    3.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    4.         <init-param>  
    5.             <param-name>encoding</param-name>  
    6.             <param-value>UTF-8</param-value>  
    7.         </init-param>  
    8.         <init-param>  
    9.             <param-name>forceEncoding</param-name>  
    10.             <param-value>true</param-value>  
    11.         </init-param>  
    12.     </filter>  
    13.   
    14.     <filter-mapping>  
    15.         <filter-name>encodingFilter</filter-name>  
    16.         <url-pattern>/*</url-pattern>  
    17.     </filter-mapping>  
  • 相关阅读:
    Google哲学(一)
    Predictably Irractional 相对论的真相
    .NET使用OpenSSL生成的pem密钥文件【做电子商务的朋友可能需要】
    从开辟蓝海到保卫蓝海(一)
    礼让?
    登门槛策略
    从开辟蓝海到保卫蓝海(四)
    盛大招聘 高级数据库开发工程师 工作地点张江高科 学历高者,经验可放宽
    Show一下拿的奖杯
    我们家的一坨和田仔玉[三色皮]
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/5790547.html
Copyright © 2011-2022 走看看