springmvc controller返回json中文乱码处理

有时候在用springmvc返回json的时候会出现中文乱码问题,其实这种问题也很好解决

 

第一种方法就是在

RequestMapping中设置,设置produce属性

 

@RequestMapping(value = "/upload", method = RequestMethod.POST,   
        produces = "text/json;charset=UTF-8")  
public @ResponseBody String upload(  
        @RequestParam("groupId") String groupId,  
        HttpServletRequest request,  
        HttpServletResponse response) {  
           //...  
       }  

 

第二种方法就是这只一个全局的,当然,如果你要是都已经快完成了肯定不会去把每个RequestMapping设置produce属性

那就需要在xml中配置一下,这样就解决了返回json的中文乱码问题

<mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

如果有问题请在评论区留言

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值