spring mvc @ResponseBody返回中文乱码解决
2016-06-12 18:22
495 查看
sping mvc中添加@ResponseBody,返回中文字符串时出现乱码,且在applicationContext-web.xml中已经配置StringHttpMessageConverter<span style="white-space:pre"> </span><!-- 避免返回中文时出现乱码 --><bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/plain;charset=UTF-8</value><value>text/html;charset=UTF-8</value></list></property></bean>
<span style="white-space:pre"> </span><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"><span style="white-space:pre"> </span><property name="messageConverters"><span style="white-space:pre"> </span><list><span style="white-space:pre"> </span><ref bean="stringHttpMessageConverter"/><span style="white-space:pre"> </span><ref bean="mappingJackson2HttpMessageConverter"/><span style="white-space:pre"> </span><ref bean="marshallingHttpMessageConverter"/><span style="white-space:pre"> </span></list><span style="white-space:pre"> </span></property><span style="white-space:pre"> </span></bean>但是出现乱码,原因是因为将<mvc:annotation-driven/>配置放在了RequestMappingHandlerAdapter配置的前边了,导致后边配置的StringHttpMessageConverter配置无效,解决办法,因为<mvc:annotation-driven/>默认也会加载RequestMappingHandlerAdapter,所以需要把<mvc:annotation-driver/>放置到RequestMappingHandlerAdapter配置的后边,中文乱码的问题解决。注:spring MVC 3.1之后,
@RequestMappingmethodscalled
RequestMappingHandlerMappingand
RequestMappingHandlerAdapter,Prior to Spring 3.1, type and method-level request mappings were examined in two separate stages — a controller was selected firstby the
DefaultAnnotationHandlerMappingandthe actual method to invoke was narrowed down second by the
AnnotationMethodHandlerAdapter.
相关文章推荐
- SpringMVC restful 注解之@RequestBody进行json与object转换
- Spring MVC @ResponseBody 中文乱码解决
- java web项目中的jsp页面发送ajax请求404错误
- 针对spring中@ResponseBody返回中文乱码的解决
- spring mvc json @ResponseBody 出现406错误 not acceptable ,解决方法,亲测可行
- @ResponseBody & @RequestBody
- @RequestBody和@ResponseBody的简单使用
- 关于@responsebody注解的说明
- Spring4.0MVC学习资料,Controller中的方法详解和使用(四)
- spring3.1中@ResponseBody返回中文乱码
- SpringMVC 流程(4)-- HttpMessageConverter
- Spring MVC之@RequestBody, @ResponseBody @PathVariable @SessionAttributes @ModelAttribute详解
- 如何把jquery 的dialog和ztree结合
- SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换(上)
- SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换(下)
- Spring MVC ResponseBody
- Spring MVC ResponseBody
- SpringMVC之@RequestBody,@ResponseBody,@RequestParam用法
- springmvc返回json数据错误
- SpingMVC4.3x教程之四@RequestBody和@ResponseBody