您的位置 首页 java

java中json传输数据乱码问题

java中json传输数据乱码问题解决方法:(推荐:java视频教程)

1、对参数先进行ISO-8859-1编码,再以utf-8解码

    @RequestMapping(method=RequestMethod.GET)    @ResponseBody    public ResponseEntity<ResultModel> searchBorrows(String borrow_name)     throws UnsupportedEncodingException{           //解决乱码问题         System.out.println("编码前===:"+borrow_name);//乱码         String borrowName=new String(borrow_name.getBytes("ISO-8859-1"),"utf-8");         System.out.println("编码后:========="+borrowName);//正常

2、如果是一般的请求,(非ajax的json**请求乱码**,直接在web.xml中配置中文过滤器) 如下:

<filter>        <filter-name>encodingFilter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>utf-8</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>encodingFilter</filter-name>        <url-pattern>/*</url-pattern><!-- 对请求项目下所有资源进行过滤-->    </filter-mapping>

在没有用springmvc时,也可添加该句解决post请求的乱码问题:request.setCharacterEncoding(“UTF-8”);
注: tomcat8已经把get请求的乱码问题解决了,tomcat7还需自己解决

3、ajax的json数据乱码

在项目中有时需要异步请求,可以在springmvc配置文件中,在注解实现的适配器和映射器标签中添加两个转换器即可,可解决对json数据请求和响应的乱码(如果tomcat编码没改,依然存在乱码问题,所有出现乱码是多方面的的)。

以下是配置spinngmvc中带的两个json转换器,实现解决json数据请求和响应乱码问题。

<!-- 注解的适配器和映射器 -->    <mvc:annotation-driven>        <mvc:message-converters>            <!--@ResponseBody 中文响应乱码 -->            <bean class="org.springframework.http.converter.StringHttpMessageConverter">                <property name="supportedMediaTypes">                    <list>                        <value>                            text/plain;charset=UTF-8                        </value>                        <value>                            text/html;charset=UTF-8                        </value>                        <value>                            application/json;charset=UTF-8                        </value>                        <value>                            application/x-www-form-urlencoded;charset=UTF-8                                             </value>                    </list>                </property>            </bean><!-- JSON中文请求乱码及解决 HttpMediaTypeNotAcceptableException: Could not find acceptable representation  异常信息-->            <bean id="jacksonMessageConverter"                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">                <property name="supportedMediaTypes">                    <list>                        <value>                            application/json;charset=UTF-8                        </value>                        <value>                            application/x-www-form-urlencoded;charset=UTF-8                                             </value>                    </list>                </property>            </bean>        </mvc:message-converters>    </mvc:annotation-driven>

更多java知识请关注java基础教程栏目。

文章来源:智云一二三科技

文章标题:java中json传输数据乱码问题

文章地址:https://www.zhihuclub.com/20222.shtml

关于作者: 智云科技

热门文章

网站地图