使用FastJSON进行实现

构造JSON的模板

public static String getJSONString(int code, String msg, Map<String, Object> map)  {
    JSONObject json = new JSONObject();
    json.put("code", code);
    json.put("msg", msg);
    if(map != null) {
        for(String key: map.keySet()) {
            json.put(key, map.get(key));
        }
    }
    return json.toJSONString();
}

处理的Controller一定要记得加上ResponseBody

@PostMapping("/add")
@ResponseBody
public String addDiscussPost(String title, String content) {
    System.out.println("addDiscussPost" + title);
    User user = HostHolder.getUser();
    if(user == null) {
        return CommunityUtil.getJSONString(403, "你还没有登陆");
    }

    DiscussPost post = new DiscussPost();
    post.setUserId(user.getId());
    post.setTitle(title);
    post.setContent(content);
    post.setCreateTime(new Date());
    discussPostService.addDiscussPost(post);
    System.out.println(post);

    return CommunityUtil.getJSONString(0, "发布成功");
}