springMVC返回json数据乱码问

时间:2023-03-09 08:55:51
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();
}