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>

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

猜你喜欢

转载自blog.csdn.net/qq_41755287/article/details/82797076