@ResponseBody 返回乱码 的完美解决办法 详解 (值得收藏)


简介:

@RequestBody

作用:

      i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;

      ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。

使用时机:

A) GET、POST方式提时, 根据request header Content-Type的值来判断:

  •     application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
  •     multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
  •     其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);

B) PUT方式提交时, 根据request header Content-Type的值来判断:

  •     application/x-www-form-urlencoded, 必须;
  •     multipart/form-data, 不能处理;
  •     其他格式, 必须;
说明:request的body部分的数据编码格式由header部分的Content-Type指定;

@ResponseBody

作用:

      该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

使用时机:

      返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;









返回结果“??”
Java代码   收藏代码
  1.        @RequestMapping(value = "/getForm")  
  2. @ResponseBody  
  3. public String getForm(String pid) {  
  4.     return "你好";  
  5. }  


返回结果“你好”
Java代码   收藏代码
  1.        @RequestMapping(value = "/getForm")  
  2. @ResponseBody  
  3. public List<String> getForm(String pid) {  
  4.     return new ArrayList<String>(){{  
  5.                   add("你好")  
  6.                }};  
  7. }  




Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  6.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  7.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">  
  8.   
  9.     <context:component-scan base-package="cn.netcluster.workflow.**" />  
  10.   
  11.     <mvc:annotation-driven />  
  12.   
  13.     <mvc:default-servlet-handler />  
  14.   
  15.     <bean  
  16.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
  17.         id="multipartResolver">  
  18.         <property name="defaultEncoding" value="UTF-8" />  
  19.         <property name="uploadTempDir" value="resources/attach/temp" />  
  20.     </bean>  
  21.   
  22. </beans>  


使用的是Spring3.1

问题补充:
xiaoZ5919 写道
你遇到的是第一个方法直接返回String会乱码,而返回list的那个不会乱码是吧?
这可以说是spring mvc的一个bug,spring MVC有一系列HttpMessageConverter去处理用@ResponseBody注解的返回值,如返回list则使用MappingJacksonHttpMessageConverter,返回string,则使用StringHttpMessageConverter,这个convert使用的是字符集是iso-8859-1,而且是final的
Java代码   收藏代码
  1. public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");  


解决的办法:

你自己重写一个StringHttpMessageConverter,使用你想要的字符集,并且使这个属性可注入

Java代码   收藏代码
  1. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  2.         <property name="messageConverters">  
  3.             <util:list>  
  4.                 <bean class="com.pcds.ecomm.website.syscustomization.ConfigurableStringHttpMessageConverter">  
  5.                     <constructor-arg value="UTF-8"/>  
  6.                 </bean>  
  7.                 <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />  
  8.             </util:list>  
  9.         </property>  
  10.     </bean>  


另外一种直接放弃String,而是使用对象


我之前是这样写的,也没有用
Java代码   收藏代码
  1. <bean  
  2.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  3.         <property name="messageConverters">  
  4.             <list>  
  5.                 <bean  
  6.                     class="org.springframework.http.converter.StringHttpMessageConverter">  
  7.                     <property name="supportedMediaTypes">  
  8.                         <list>  
  9.                             <value>text/plain;charset=UTF-8</value>  
  10.                         </list>  
  11.                     </property>  
  12.                 </bean>  
  13.                 <bean  
  14.                     class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />  
  15.             </list>  
  16.         </property>  
  17.     </bean>  


问题补充:
xiaoZ5919 写道
Java代码   收藏代码
  1. if (writeAcceptCharset) {  
  2.             outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());  
  3.         }  
  4.         MediaType contentType = outputMessage.getHeaders().getContentType();  
  5.         Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;  
  6.         FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset));  

兄弟我一直都设置了这个
Java代码   收藏代码
  1.     <filter>  
  2.         <filter-name>CharacterEncodingFilter</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. <filter-mapping>  
  14.         <filter-name>CharacterEncodingFilter</filter-name>  
  15.         <url-pattern>/*</url-pattern>  
  16.     </filter-mapping>  

问题补充:
xiaoZ5919 写道
兄弟 也许我没有说清楚,
需要设置的是response.setContentType("text/html; charset=UTF-8")
而不是response.setCharacterEncoding(this.encoding);

你看看我贴的代码就知道了

Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;

那个filter里面设置的是

response.setCharacterEncoding(this.encoding);

不过我觉得这不是一个好的办法

最好的办法还得是重写一个StringHttpMessageConverter,使其能灵活配置charset

xiaoZ5919 写道
兄弟 也许我没有说清楚,
需要设置的是response.setContentType("text/html; charset=UTF-8")
而不是response.setCharacterEncoding(this.encoding);

你看看我贴的代码就知道了

Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;

那个filter里面设置的是

response.setCharacterEncoding(this.encoding);

不过我觉得这不是一个好的办法

最好的办法还得是重写一个StringHttpMessageConverter,使其能灵活配置charset


兄弟还是不行哦
Java代码   收藏代码
  1. @RequestMapping(value = "/getForm")  
  2. @ResponseBody  
  3. public String getForm(HttpServletResponse response, String pid) {  
  4.     response.setContentType("text/html; charset=UTF-8");  
  5.     System.out.println(super.formService.getRenderedStartForm(pid)  
  6.             .toString());  
  7.     return "你好";  



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值