版权声明:本文为博主原创文章,未经博主允许不得转载。
返回的结果中,中文全部被问号(?)代替的解决办法:
*-servlet.xml的部分配置如下:
- <bean id="utf8Charset" class="java.nio.charset.Charset"
- factory-method="forName">
- <constructor-arg value="UTF-8"/>
- </bean>
- <mvc:annotation-driven>
- <mvc:message-converters>
- <bean class="org.springframework.http.converter.StringHttpMessageConverter">
- <constructor-arg ref="utf8Charset" />
- </bean>
- </mvc:message-converters>
- </mvc:annotation-driven>
思路非常简单:查看StringHttpMessageConverter可知其有两个构造函数,
- public StringHttpMessageConverter() {
- this(DEFAULT_CHARSET);
- }
- public StringHttpMessageConverter(Charset defaultCharset) {
- super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);
- this.defaultCharset = defaultCharset;
- this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());
- }
这里采用第二个构造函数向其中注入我们的utf8Charset
上面配置会覆盖 mvc:annotation-driven 默认配置的StringHttpMessageConverter,其余的MessageConverter应该不会受到影响。
具体文档描述如下:
- Element : message-converters
- Configures one or more HttpMessageConverter types to use for converting @RequestBody method parameters and
- @ResponseBody method return values. Using this configuration element is optional. HttpMessageConverter
- registrations provided here will take precedence over HttpMessageConverter types registered by default. Also see
- the register-defaults attribute if you want to turn off default registrations entirely.
<mvc:annotation-driven>会默认配置一些HttpMessageConverter,Spring Reference描述如下:
- HttpMessageConverter support for @RequestBody method parameters and @ResponseBody method return values from @RequestMapping or @ExceptionHandler methods.
- This is the complete list of HttpMessageConverters set up by mvc:annotation-driven:
- ByteArrayHttpMessageConverter converts byte arrays.
- StringHttpMessageConverter converts strings.
- ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.
- SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.
- FormHttpMessageConverter converts form data to/from a MultiValueMap<String, String>.
- Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present on the classpath.
- MappingJackson2HttpMessageConverter (or MappingJacksonHttpMessageConverter) converts to/from JSON — added if Jackson 2 (or Jackson) is present on the classpath.
- AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.
- RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.
返回的结果中,中文全部是乱码(非问号)的解决办法:
在web.xml中加入以下Filter:
- <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>