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();
    }    

 

posted @ 2019-06-29 09:09  全栈九九六  阅读(394)  评论(0编辑  收藏  举报