我想知道某些标准 Java 类库中是否有一个枚举类型,它为所有有效的 HTTP 响应代码定义了符号常量。它应该支持与相应整数值之间的转换。
我正在调试一些使用
javax.ws.rs.core.Response.Status
的 Java 代码。它可以工作,但它只定义了大约一半的有效 HTTP 响应代码。
HttpURLConnection
缺少很多代码,例如 HTTP 100/Continue
。
Apache HttpComponents 中有一个完整的列表:
org.apache.http.HttpStatus
(从 Apache Http 客户端替换了 org.apache.commons.HttpClient.HttpStatus
,已达到生命周期终点)
嗯,在 HttpURLConnection class
中有精确整数值的静态常量Servlet API 的接口
javax.servlet.http.HttpServletResponse
具有 int
常量名称 SC_<description>
形式的所有响应代码。请参阅 http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html
如果您使用 Spring,3.x 版本可以满足您的需求: http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/http/HttpStatus.html
这是一个包含状态代码及其描述的枚举(在撰写本文时)对应于 HTTP 状态代码注册表。
请注意,注册表可能会更新,并且有时会使用非官方状态代码。
public enum HttpStatusCode {
//1xx: Informational
CONTINUE(100, "Continue"),
SWITCHING_PROTOCOLS(101, "Switching Protocols"),
PROCESSING(102, "Processing"),
EARLY_HINTS(103, "Early Hints"),
//2xx: Success
OK(200, "OK"),
CREATED(201, "Created"),
ACCEPTED(202, "Accepted"),
NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information"),
NO_CONTENT(204, "No Content"),
RESET_CONTENT(205, "Reset Content"),
PARTIAL_CONTENT(206, "Partial Content"),
MULTI_STATUS(207, "Multi-Status"),
ALREADY_REPORTED(208, "Already Reported"),
IM_USED(226, "IM Used"),
//3xx: Redirection
MULTIPLE_CHOICES(300, "Multiple Choice"),
MOVED_PERMANENTLY(301, "Moved Permanently"),
FOUND(302, "Found"),
SEE_OTHER(303, "See Other"),
NOT_MODIFIED(304, "Not Modified"),
USE_PROXY(305, "Use Proxy"),
TEMPORARY_REDIRECT(307, "Temporary Redirect"),
PERMANENT_REDIRECT(308, "Permanent Redirect"),
//4xx: Client Error
BAD_REQUEST(400, "Bad Request"),
UNAUTHORIZED(401, "Unauthorized"),
PAYMENT_REQUIRED(402, "Payment Required"),
FORBIDDEN(403, "Forbidden"),
NOT_FOUND(404, "Not Found"),
METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
NOT_ACCEPTABLE(406, "Not Acceptable"),
PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required"),
REQUEST_TIMEOUT(408, "Request Timeout"),
CONFLICT(409, "Conflict"),
GONE(410, "Gone"),
LENGTH_REQUIRED(411, "Length Required"),
PRECONDITION_FAILED(412, "Precondition Failed"),
REQUEST_TOO_LONG(413, "Payload Too Large"),
REQUEST_URI_TOO_LONG(414, "URI Too Long"),
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
REQUESTED_RANGE_NOT_SATISFIABLE(416, "Range Not Satisfiable"),
EXPECTATION_FAILED(417, "Expectation Failed"),
MISDIRECTED_REQUEST(421, "Misdirected Request"),
UNPROCESSABLE_ENTITY(422, "Unprocessable Entity"),
LOCKED(423, "Locked"),
FAILED_DEPENDENCY(424, "Failed Dependency"),
TOO_EARLY(425, "Too Early"),
UPGRADE_REQUIRED(426, "Upgrade Required"),
PRECONDITION_REQUIRED(428, "Precondition Required"),
TOO_MANY_REQUESTS(429, "Too Many Requests"),
REQUEST_HEADER_FIELDS_TOO_LARGE(431, "Request Header Fields Too Large"),
UNAVAILABLE_FOR_LEGAL_REASONS(451, "Unavailable For Legal Reasons"),
//5xx: Server Error
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
NOT_IMPLEMENTED(501, "Not Implemented"),
BAD_GATEWAY(502, "Bad Gateway"),
SERVICE_UNAVAILABLE(503, "Service Unavailable"),
GATEWAY_TIMEOUT(504, "Gateway Timeout"),
HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version Not Supported"),
VARIANT_ALSO_NEGOTIATES(506, "Variant Also Negotiates"),
INSUFFICIENT_STORAGE(507, "Insufficient Storage"),
LOOP_DETECTED(508, "Loop Detected"),
NOT_EXTENDED(510, "Not Extended"),
NETWORK_AUTHENTICATION_REQUIRED(511, "Network Authentication Required");
private final int value;
private final String description;
HttpStatusCode(int value, String description) {
this.value = value;
this.description = description;
}
public int getValue() {
return value;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return value + " " + description;
}
public static HttpStatusCode getByValue(int value) {
for(HttpStatusCode status : values()) {
if(status.value == value) return status;
}
throw new IllegalArgumentException("Invalid status code: " + value);
}
}
每个人似乎都忽略了你问题的“枚举类型”部分。
虽然没有 HTTP 状态代码的规范源,但有一种简单的方法可以将您需要的任何缺失的状态常量 添加 到
javax.ws.rs.core.Response.Status
提供的状态常量,而无需向项目添加任何其他依赖项。
javax.ws.rs.core.Response.Status
只是 javax.ws.rs.core.Response.StatusType
接口的一种实现。您只需使用所需状态代码的定义创建自己的实现枚举即可。
Javax、Jersey 等核心库是针对 接口
StatusType
编写的,而不是实现 Status
(或者它们当然应该)。由于您的新 Status 枚举实现了 StatusType
,因此它可以在任何需要使用
javax.ws.rs.core.Response.Status
常量的地方使用。请记住,您自己的代码也应该写入
StatusType
接口。这将使您能够使用自己的状态代码以及“标准”状态代码。这里有一个简单实现的要点,其中包含为“信息 1xx”状态代码定义的常量:
Netty,您可以使用:
示例:
javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED //401
javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR //500
文字,如果您只有代码,可以使用:
org.apache.http.impl.EnglishReasonPhraseCatalog.INSTANCE.getReason(httpCode,null)
其中
httpCode
是您从 HTTP 响应中获取的原因代码。请参阅
https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/impl/EnglishReasonPhraseCatalog.html了解详细信息
2)要获取原因代码,如果您只有文本,可以使用BasicHttpResponse
。请参阅此处了解详细信息:
https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/message/BasicHttpResponse.html
HttpStatus.values()
图书馆:
org.glassfish.grizzly.http.util.HttpStatus
public static final HttpStatus CONINTUE_100 = register(100, "Continue");
public static final HttpStatus SWITCHING_PROTOCOLS_101 = register(101, "Switching Protocols");
public static final HttpStatus WEB_SOCKET_PROTOCOL_HANDSHAKE_101 = register(101, "Web Socket Protocol Handshake");
public static final HttpStatus OK_200 = register(200, "OK");
public static final HttpStatus CREATED_201 = register(201, "Created");
public static final HttpStatus ACCEPTED_202 = register(202, "Accepted");
public static final HttpStatus NON_AUTHORATIVE_INFORMATION_203 = register(203, "Not-Authoritative Information");
public static final HttpStatus NO_CONTENT_204 = register(204, "No Content");
public static final HttpStatus RESET_CONTENT_205 = register(205, "Reset Content");
public static final HttpStatus PARTIAL_CONTENT_206 = register(206, "Partial Content");
public static final HttpStatus MULTIPLE_CHOICES_300 = register(300, "Multiple Choices");
public static final HttpStatus MOVED_PERMANENTLY_301 = register(301, "Moved Permanently");
public static final HttpStatus FOUND_302 = register(302, "Found");
public static final HttpStatus SEE_OTHER_303 = register(303, "See Other");
public static final HttpStatus NOT_MODIFIED_304 = register(304, "Not Modified");
public static final HttpStatus USE_PROXY_305 = register(305, "Use Proxy");
public static final HttpStatus TEMPORARY_REDIRECT_307 = register(307, "Temporary Redirect");
public static final HttpStatus PERMANENT_REDIRECT_308 = register(308, "Permanent Redirect");
public static final HttpStatus BAD_REQUEST_400 = register(400, "Bad Request");
public static final HttpStatus UNAUTHORIZED_401 = register(401, "Unauthorized");
public static final HttpStatus PAYMENT_REQUIRED_402 = register(402, "Payment Required");
public static final HttpStatus FORBIDDEN_403 = register(403, "Forbidden");
public static final HttpStatus NOT_FOUND_404 = register(404, "Not Found");
public static final HttpStatus METHOD_NOT_ALLOWED_405 = register(405, "Method Not Allowed");
public static final HttpStatus NOT_ACCEPTABLE_406 = register(406, "Not Acceptable");
public static final HttpStatus PROXY_AUTHENTICATION_REQUIRED_407 = register(407, "Proxy Authentication Required");
public static final HttpStatus REQUEST_TIMEOUT_408 = register(408, "Request Timeout");
public static final HttpStatus CONFLICT_409 = register(409, "Conflict");
public static final HttpStatus GONE_410 = register(410, "Gone");
public static final HttpStatus LENGTH_REQUIRED_411 = register(411, "Length Required");
public static final HttpStatus PRECONDITION_FAILED_412 = register(412, "Precondition Failed");
public static final HttpStatus REQUEST_ENTITY_TOO_LARGE_413 = register(413, "Request Entity Too Large");
public static final HttpStatus REQUEST_URI_TOO_LONG_414 = register(414, "Request-URI Too Long");
public static final HttpStatus UNSUPPORTED_MEDIA_TYPE_415 = register(415, "Unsupported Media Type");
public static final HttpStatus REQUEST_RANGE_NOT_SATISFIABLE_416 = register(416, "Request Range Not Satisfiable");
public static final HttpStatus EXPECTATION_FAILED_417 = register(417, "Expectation Failed");
public static final HttpStatus MISDIRECTED_REQUEST = register(421, "Misdirected Request");
public static final HttpStatus REQUEST_HEADER_FIELDS_TOO_LARGE = register(431, "Request Header Fields Too Large");
public static final HttpStatus INTERNAL_SERVER_ERROR_500 = register(500, "Internal Server Error");
public static final HttpStatus NOT_IMPLEMENTED_501 = register(501, "Not Implemented");
public static final HttpStatus BAD_GATEWAY_502 = register(502, "Bad Gateway");
public static final HttpStatus SERVICE_UNAVAILABLE_503 = register(503, "Service Unavailable");
public static final HttpStatus GATEWAY_TIMEOUT_504 = register(504, "Gateway Timeout");
public static final HttpStatus HTTP_VERSION_NOT_SUPPORTED_505 = register(505, "HTTP Version Not Supported");
只是
import software.amazon.awssdk.http.HttpStatusCode;
例如。
HttpStatusCode.INTERNAL_SERVER_ERROR
500
HttpStatusCode.OK
200
HttpStatus
类,它为您提供各种 Http 状态作为常量。