@RequestMapping的produces方法
第一种解决方案是使用@RequestMapping注解的produces方法。写法如下:
@RequestMapping(value = "testPersonalValidtor.do",produces = "application/json;charset=utf-8")在方法上加上这个注解就可以了。但是这样写的话有限制,只能在特定的方法上面使用。如果需要全局都使用的话,需要修改SpringMVC的配置文件。
使用messageConverters
第二种解决办法是使用HttpMessageConverter接口的相关实现类。我们先看配置文件中的配置信息。
bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
本文介绍在SpringMVC框架中如何通过两种方式设置应用返回JSON格式的内容,并确保字符集为UTF-8,一种是在具体的方法上使用@RequestMapping注解进行设置,另一种是通过配置HttpMessageConverter接口的实现类来全局生效。
2027

被折叠的 条评论
为什么被折叠?



