springmvc 接口返回类转为json,并处理中文乱码问题

在springmvc.xml中加入下面一段

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <!-- 用于避免响应头过大 -->
                <property name="writeAcceptCharset" value="false"/>
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

如果只是解决中文乱码问题只要加入下面一段

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

原文地址:https://www.cnblogs.com/blackl/p/10579934.html

时间: 2024-08-11 09:57:35

springmvc 接口返回类转为json,并处理中文乱码问题的相关文章

POST JSON (字典 自定义类转为JSON上传)

/// POST JSON 的主方法 主要设置  Content-Type 为  application/json 剩下就是将 类转为JSON 格式二进制数 - (void)postJSON:(NSData *)data { // 1. url NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/postjson.php"]; // 2. request NSMutableURLRequest *request = [NSMutab

springmvc 4.x 处理json 数据时中文乱码

原因: springmvc在处理请求时,默认采用的是 ISO-8859-1 编码格式,具体原因不了解,个人觉得是还没有来得及更改,所以在处理一些json格式的时候,会出现中文乱码. org.springframework.http.converter.StringHttpMessageConverter类是处理请求或相应字符串的类,并且默认字符集为ISO-8859-1,所以在当返回json中有中文时会出现乱码. 解决办法,只需在配置文件中加入如下代码: <!-- 处理请求时返回json字符串的中

spring boot 解决后台返回 json 到前台中文乱码之后出现返回json数据报错 500:no convertter for return value of type

问题描述 spring Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson spring boot 新增配置解决后台返回 json 到前台中文乱码之后,出现返回json数据报错:no convertter for return value of type 注释掉解决中文乱码的问题之后返回对象json正常 Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWr

response 返回js的alert()语句,中文乱码如何解决

response 返回js的alert()语句,中文乱码如何解决, 步骤1:在后台加上如下代码: response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8;"); 完整参考代码如下: response.setCharacterEncoding("utf-8"); response.setContentType("tex

springmvc提供的解决post/get请求参数中文乱码问题

<!--springmvc提供的解决post/get请求参数中文乱码问题--><filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</p

Asp.Net WebAPI配置接口返回数据类型为Json格式

一.默认情况下WebApi 对于没有指定请求数据类型类型的请求,返回数据类型为Xml格式 例如:从浏览器直接输入地址,或者默认的XMLRequest,或者AngularJs的get请求等. 对于有循环引用的也会抛出异常""ObjectContent`1"类型未能序列化内容类型"application/xml; charset=utf-8"的响应正文." 二.设置返回格式为Json数据 1.修改配置,这对所有的接口都生效 找到Global.asax

httpclient工具类,post请求发送json字符串参数,中文乱码处理

在使用httpclient发送post请求的时候,接收端中文乱码问题解决. 正文: 我们都知道,一般情况下使用post请求是不会出现中文乱码的.可是在使用httpclient发送post请求报文含中文的时候在发送端数据正常但是到了服务器端就中文乱码了. 解决办法: 发送端进行设置编码如下: 工具类: 1 package com.Util; 2 3 import com.google.common.base.Charsets; 4 import org.apache.http.HttpEntity

json传输数据解决中文乱码问题

1.Ajax在url带参数(中文): encodeURI(encodeURI(expireDesc))//设置编码 2.后台接收需要转码: URLDecoder.decode(expireDesc, "UTF-8") //将接收的参数转码 3.例子:js Ajax: function exchange(expireDesc){            $.ajax({             type: "post",               url:ctx+&q

数据接口返回类餐参考

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TheBiz.Common { public interface IResult { /// <summary> /// 返回值 /// </summary> EResult Code { set; get; } /// <summary> /// 错误原因 /// </su