spring @ResponseBody 乱码问题解决方案

浏览器请求或者restful工具请求乱码,但是ajax请求可能不乱码哦,可以用下面的js测试一下:
<html>
<head>
    <meta charset="utf-8">
    <title>测试乱码</title>
    <script type="text/javascript" src="jquery.min.js"></script>
	<script>
	$.ajax({
        url: "http://127.0.0.1:8080/login",
        type: 'POST',
        cache: false,
        data: '',
        processData: false,
        contentType: false,
        dataType: "json",
        beforeSend: function () {
           
        },
        success: function (result) {
			alert(result.message);
        }
    });
</script>
</head>
<body >


</body>
</html>

要想所有的都不乱码,可以@RequestMapping添加:

produces = "application/json; charset=utf-8"

比如:

@RequestMapping(value = "/login", method = { RequestMethod.GET, RequestMethod.POST } ,produces = "application/json; charset=utf-8")
或者:  
      <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>


如果这个时候返回的json字符串多了好多“\”可以理解为多转换了一次json字符串,检查一下是不是多配置一个jsonMessageConverter。




猜你喜欢

转载自blog.csdn.net/plm609337931/article/details/80065934