springMVC返回json数据乱码问

Posted yysbolg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springMVC返回json数据乱码问相关的知识,希望对你有一定的参考价值。

在springMVC controller中返回json数据出现乱码问题,因为没有进行编码,只需要简单的注解就可以了

在@RequestMapping()中加入produces="text/html;charset=UTF-8"属性即可,如下:

    @RequestMapping(value="/respost",method=RequestMethod.GET,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String postList(@RequestParam("topicId") String topicId)
        List<Post> posts=new ArrayList<Post>();
        System.out.println("topicId-----"+topicId);
        posts=postService.findPostList(topicId);
        JSONArray postJson=JSONArray.fromObject(posts);
        return postJson.toString();
        

 

以上是关于springMVC返回json数据乱码问的主要内容,如果未能解决你的问题,请参考以下文章