java状态码枚举类
时间: 2023-11-21 16:58:46 浏览: 174
以下是一个Java状态码枚举类的例子:
```java
public enum HttpStatus {
OK(200, "OK"),
BAD_REQUEST(400, "Bad Request"),
UNAUTHORIZED(401, "Unauthorized"),
FORBIDDEN(403, "Forbidden"),
NOT_FOUND(404, "Not Found"),
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
SERVICE_UNAVAILABLE(503, "Service Unavailable");
private final int code;
private final String reason;
HttpStatus(int code, String reason) {
this.code = code;
this.reason = reason;
}
public int getCode() {
return code;
}
public String getReason() {
return reason;
}
}
```
这个枚举类定义了一些常见的HTTP状态码,包括状态码的数字代码和原因短语。你可以使用这个枚举类来表示HTTP响应的状态码,例如:
```java
HttpStatus status = HttpStatus.OK;
int code = status.getCode(); // 200
String reason = status.getReason(); // "OK"
```
阅读全文