2012-03-02 110 views
1

@ResponseBody的默認字符集是iso-8859-1, 如何更改爲utf8?springMVC3.0響應正文編碼問題

下面的配置似乎不起作用。

<bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
     <property name = "messageConverters"> 
      <list> 
       <bean class = "org.springframework.http.converter.StringHttpMessageConverter"> 
        <property name = "supportedMediaTypes"> 
         <list> 
         <value>text/plain;charset=UTF-8</value> 
         </list> 
        </property> 
       </bean> 
      </list> 
     </property> 
    </bean> 
+4

「我單獨使用springmvc(沒有彈簧)」 - 不,你不知道。 – skaffman 2012-03-02 09:30:38

+0

查看這裏的討論:http://forum.springsource.org/showthread.php?81858-ResponseBody-and-UTF-8特別是springbee的帖子 – Waqas 2012-03-04 09:38:13

+0

感謝您的鏈接。 「沒有彈簧」我的意思是我沒有在web.xml中配置spring,只是DispatcherServlet – tgf2 2012-03-05 01:56:58

回答

3

您可以添加產生=「text/plain的;字符集= UTF-8" ,以請求映射

@RequestMapping(value = "/rest/create/document", produces = "text/plain;charset=UTF-8") 
@ResponseBody 
public void create(Document document, HttpServletRespone respone) throws UnsupportedEncodingException { 

    Document newDocument = DocumentService.create(Document); 

    return jsonSerializer.serialize(newDocument); 
} 

see this blog for more detail