关于如何自定义SpringBoot返回的状态码
关于HttpStatus
在SpringBoot中关于状态码有一个枚举类型,如下。
public enum HttpStatus {
CONTINUE(100, "Continue"),
SWITCHING_PROTOCOLS(101, "Switching Protocols"),
PROCESSING(102, "Processing"),
CHECKPOINT(103, "Checkpoint"),
OK(200, "OK"),
...
}
1
2
3
4
5
6
7
8
通过以下三种方法均可实现
1. 通过@ResponseStatus注解
eg:
@ResponseStatus(code=HttpStatus.INTERNAL_SERVER_ERROR,reason="server error")
public class ServerException extends Exception {
...
}
1
2
3
4
在全局异常捕捉的类中定义,可以自定义特定的异常来返回特定的状态码。
2. 修改response的status属性
eg:
@RequestMapping(value = "/index", method = RequestMethod.GET)
public void index(HttpServletResponse response) throws IOException{
response.setStatus(500);
response.getWriter().append("server error");
}
1
2
3
4
5
需要多加response参数,但这样的控制器都必须为void,有诸多限制。
3. ResponseEntity
eg:
@RequestMapping(value = "/user", method = RequestMethod.GET)
public ResponseEntity<Map<String,Object>> getUser() throws IOException{
Map<String,Object> map = new HashMap<String,Object>();
map.put("name", "user");
return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
}
1
2
3
4
5
6
这样返回类型就只能是ResponseEntity。
前后端分离开发一般都会自定义返回的数据格式。
都会包含msg,code,data三个属性。
可用上述思路实现以下工具类。实现自定义返回的状态码,需将返回格式设置为以下类的格式,将返回数据传入data。
JSONResult.java
public class JSONResult<T> extends ResponseEntity<Message> {
public JSONResult(HttpStatus status) {
super(status);
}
public JSONResult(String code, String msg, T data) {
super(Message.custom(code, msg, data), Message.num2HttpStatus(code));
}
public JSONResult(String code, String msg) {
super(Message.custom(code, msg), Message.num2HttpStatus(code));
}
public static <T> JSONResult<T> success(T data) {
return new JSONResult("200", "成功", data);
}
public static <T> JSONResult<T> failed(T data) {
return new JSONResult("422", "失败", data);
}
public static <T> JSONResult<T> failMsg(String msg) {
return new JSONResult("422", msg);
}
public static <T> JSONResult<T> custom(String code, String msg, T data) {
return new JSONResult(code, msg, data);
}
}
class Message<T> {
String status;
//向前端返回的内容
String message;
T data;
public Message() {
}
public Message(String status, String message) {
this.status = status;
this.message = message;
}
public Message(String status, String message, T data) {
this.data = data;
this.status = status;
this.message = message;
}
public static <T> Message<T> custom(String status, String message, T data) {
return new Message(status, message, data);
}
public static <T> Message<T> custom(String status, String message) {
return new Message(status, message);
}
public static HttpStatus num2HttpStatus(String code) {
HttpStatus status = HttpStatus.NOT_FOUND;
for (HttpStatus httpStatus : HttpStatus.values()) {
boolean b = Integer.parseInt(code) == httpStatus.value();
if (b) {
return httpStatus;
}
}
return status;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
通过继承ResponseEntity类来实现自定义返回类的自定义状态码返回。