Java 是否有完整的 HTTP 响应代码枚举?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/730283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Java have a complete enum for HTTP response codes?
提问by Zoltan Szilagyi
I'm wondering if there is an enum type in some standard Java class library that defines symbolic constants for all of the valid HTTP response codes. It should support conversion to/from the corresponding integer values.
我想知道在某些标准 Java 类库中是否有一个枚举类型,它为所有有效的 HTTP 响应代码定义了符号常量。它应该支持到/从相应的整数值的转换。
I'm debugging some Java code that uses javax.ws.rs.core.Response.Status. It works, but it only defines about half of the valid HTTP response codes.
我正在调试一些使用 javax.ws.rs.core.Response.Status 的 Java 代码。它有效,但它只定义了大约一半的有效 HTTP 响应代码。
采纳答案by John Feminella
I don't think there's one that's complete in the standard Java classes; HttpURLConnection
is missing quite a few codes, like HTTP 100/Continue
.
我认为标准 Java 类中没有一个是完整的;HttpURLConnection
缺少很多代码,例如HTTP 100/Continue
.
There's a complete list in the Apache HttpComponents, though:org.apache.http.HttpStatus
(replaced org.apache.commons.HttpClient.HttpStatus
from Apache Http Client, which reached end of life)
不过,Apache HttpComponents 中有一个完整的列表:(由 Apache Http Clientorg.apache.http.HttpStatus
替换org.apache.commons.HttpClient.HttpStatus
,已终止)
回答by Mystic
Well, there are static constants of the exact integer values in the HttpURLConnection class
嗯,在HttpURLConnection类中有精确整数值的静态常量
回答by David Rabinowitz
The Interface javax.servlet.http.HttpServletResponse
from the servlet API has all the response codes in the form of int
constants names SC_<description>
. See http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html
接口javax.servlet.http.HttpServletResponse
从servlet API具有形式的所有响应代码int
常量名SC_<description>
。见http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html
回答by Greg Noe
Also check out the Restlet Status class:
另请查看 Restlet Status 类:
http://www.restlet.org/documentation/1.1/api/org/restlet/data/Status.html
http://www.restlet.org/documentation/1.1/api/org/restlet/data/Status.html
回答by Ed J
If you're using Spring, the 3.x release has what your looking for: http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/http/HttpStatus.html
如果您使用的是 Spring,则 3.x 版本有您想要的:http: //static.springsource.org/spring/docs/3.0.x/api/org/springframework/http/HttpStatus.html
回答by Andrés Canavesi
Use javax.servlet.http.HttpServletResponse class
使用 javax.servlet.http.HttpServletResponse 类
Example:
例子:
javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED //401
javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR //500
回答by user1050755
The best provider for http status code constants is likely to be Jetty's org.eclipse.jetty.http.HttpStatus class because:
http 状态代码常量的最佳提供者可能是 Jetty 的 org.eclipse.jetty.http.HttpStatus 类,因为:
- there is a javadoc package in maven which is important if you search for the constant and only know the number -> just open the api docs page and search for the number
- the constants contain the status code number itself.
- maven 中有一个 javadoc 包,如果您搜索常量并且只知道数字,这很重要 -> 只需打开 api 文档页面并搜索数字
- 常量包含状态代码本身。
Only thing I would improve: put the status code number in front of the text description in order to make auto-completion lookup more convient when you are starting with the code.
我唯一要改进的是:将状态代码编号放在文本描述的前面,以便在您开始使用代码时使自动完成查找更加方便。
回答by ak123
Another option is to use HttpStatus
class from the Apache commons-httpclient which provides you the various Http statuses as constants.
另一种选择是使用HttpStatus
Apache commons-httpclient 中的类,它为您提供各种 Http 状态作为常量。
回答by Andrejs
If you are using Netty, you can use:
如果您使用的是Netty,则可以使用:
- io.netty.handler.codec.http.HttpResponseStatus
- io.netty.handler.codec.http。HttpResponse 状态
回答by David Avendasora
Everyone seems to be ignoring the "enum type" portion of your question.
每个人似乎都忽略了问题的“枚举类型”部分。
While there is no canonical source for HTTP Status Codes there is an simple way to addany missing Status constants you need to those provided by javax.ws.rs.core.Response.Status
without adding any additional dependencies to your project.
虽然没有 HTTP 状态代码的规范来源,但有一种简单的方法可以将任何缺少的状态常量添加到提供的状态常量中,javax.ws.rs.core.Response.Status
而无需向项目添加任何额外的依赖项。
javax.ws.rs.core.Response.Status
is just one implementation of the javax.ws.rs.core.Response.StatusType
interface. You simply need to create your own implementation enum with definitions for the Status Codes that you want.
javax.ws.rs.core.Response.Status
只是javax.ws.rs.core.Response.StatusType
接口的一种实现。您只需要创建自己的实现枚举,其中包含您想要的状态代码的定义。
Core libraries like Javax, Jersey, etc. are written to the interfaceStatusType
not the implementationStatus
(or they certainly shouldbe). Since your new Status enum implements StatusType
it can be used anyplace you would use a javax.ws.rs.core.Response.Status
constant.
Javax、Jersey 等核心库被写入接口StatusType
而不是实现Status
(或者它们当然应该是)。由于您的新 Status 枚举实现了StatusType
它,因此可以在您使用javax.ws.rs.core.Response.Status
常量的任何地方使用它。
Just remember that your own code should also be written to the StatusType
interface. This will enable you to use both your own Status Codes along side the "standard" ones.
请记住,您自己的代码也应该写入StatusType
接口。这将使您能够将自己的状态代码与“标准”状态代码一起使用。
Here's a gist with a simple implementation with constants defined for the "Informational 1xx" Status Codes: https://gist.github.com/avendasora/a5ed9acf6b1ee709a14a
这是一个带有为“信息 1xx”状态代码定义的常量的简单实现的要点:https: //gist.github.com/avendasora/a5ed9acf6b1ee709a14a