response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter out = response.getWriter();
JSONObject res = new JSONObject();
res.put("code", 1001);
res.put("message", "返回信息");
res.put("success", true);
out.append(res.toString());
// 引入的包
import java.io.IOException;
import java.io.PrintWriter;
import com.alibaba.fastjson.JSONObject;
// 返回
{
"code": 1001,
"success": true,
"message": "用户TOKEN失效"
}
本文详细介绍了如何使用Java在HTTP响应中设置UTF-8编码和JSON内容类型,通过PrintWriter输出包含状态码、成功标识及消息的JSON对象。特别讨论了当用户TOKEN失效时,如何返回特定错误信息。
2404

被折叠的 条评论
为什么被折叠?



