我想知道在某些标准Java类库中是否存在枚举类型,为所有有效的HTTP响应代码定义符号常量。它应该支持与对应的整数值之间的转换。
我正在调试一些使用javax.ws.rs.core.Response.Status的Java代码。它可以工作,但它只定义了大约一半的有效HTTP响应代码。
javax.ws.rs.core.Response.Status
嗯,在__abc0类中有精确整数值的静态常量
我认为在标准Java类中没有一个是完整的;HttpURLConnection缺少一些代码,比如HTTP 100/Continue。
HttpURLConnection
HTTP 100/Continue
org.apache.http.HttpStatus
org.apache.commons.HttpClient.HttpStatus
来自servlet API的接口javax.servlet.http.HttpServletResponse具有所有以int常量名SC_<description>形式出现的响应代码。看到http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html
javax.servlet.http.HttpServletResponse
int
SC_<description>
还可以查看Restlet Status类:
http://www.restlet.org/documentation/1.1/api/org/restlet/data/Status.html
如果你正在使用Spring, 3。X版本有你想要的: http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/http/HttpStatus.html < / p >
使用javax.servlet.http.HttpServletResponse类
例子:
javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED //401 javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR //500
http状态码常量的最佳提供者可能是Jetty的org.eclipse.jetty.http.HttpStatus类,因为:
我唯一需要改进的是:将状态代码放在文本描述的前面,以便在开始编写代码时更方便地进行自动完成查找。
另一个选择是使用Apache common -httpclient中的HttpStatus类,它为你提供各种Http状态作为常量。
HttpStatus
如果你正在使用网状的,你可以使用:
每个人似乎都忽略了你问题中的“枚举类型”部分。
虽然没有规范的HTTP状态码源,但有一种简单的方法可以将你需要的状态常量添加替换为javax.ws.rs.core.Response.Status提供的状态常量,而不需要为你的项目添加任何额外的依赖关系。
javax.ws.rs.core.Response.Status只是javax.ws.rs.core.Response.StatusType接口的一个实现。您只需要创建自己的实现枚举,其中包含所需的Status Codes的定义。
javax.ws.rs.core.Response.StatusType
核心库如Javax, Jersey等被写入接口 StatusType,而不是实现 Status(或者它们肯定是应该)。由于你的新状态枚举实现了StatusType,它可以在任何你使用javax.ws.rs.core.Response.Status常量的地方使用。
StatusType
Status
只要记住,你自己的代码也应该被写入StatusType接口。这将使您能够使用您自己的状态代码以及“标准”状态代码。
下面是一个简单实现的要点,其中为“Informational 1xx”状态代码定义了常量:https://gist.github.com/avendasora/a5ed9acf6b1ee709a14a
1)如果你只有代码,要得到文本的原因,你可以使用:
org.apache.http.impl.EnglishReasonPhraseCatalog.INSTANCE.getReason(httpCode,null)
其中httpCode将是你从HTTP响应中获得的原因代码。
httpCode
详见https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/impl/EnglishReasonPhraseCatalog.html
2)如果只有文本,要得到代码的原因,可以使用BasicHttpResponse。
BasicHttpResponse
详细信息请参见这里:https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/message/BasicHttpResponse.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); } }
请看下面Spring框架中的枚举,它提供了所有HTTP响应状态代码
HttpStatus.values ()