HTTP401状态码返回

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
HttpServletResponse response = (HttpServletResponse) servletResponse;

response.setContentType("application/json;charset=UTF-8");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

PrintWriter out = response.getWriter();
Map<String,Object> map = new HashMap<String,Object>();
map.put("code",401);
map.put("message","未登录");
out.write(objectMapper.writeValueAsString(map));
out.flush();
out.close();

返回结果

1
2
3
4
{
"code": 401,
"message": "未登录"
}

最后

本文到此结束,感谢阅读。如果您觉得不错,请关注公众号【当我遇上你】,您的支持是我写作的最大动力。