@ResponseBody 返回亂碼 的完美解決辦法 詳解 (值得收藏)


返回結果“??”
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 "你好";  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章