springMVC3.0响应体编码问题

时间:2012-03-02 09:28:34

标签: spring-mvc

@ResponseBody的默认字符集是iso-8859-1, 如何更改为utf8?

下面的

配置似乎不起作用。

<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>

2 个答案:

答案 0 :(得分:3)

您可以添加produce =“text / plain; charset = 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

答案 1 :(得分:0)

如果您使用的是tomcat,还需要在Tomcat server.xml配置中的URIEncoding="UTF-8"上指定<Connector>,如下所述:

http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8