目录(?)[+]

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

*-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>