Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

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.

share|improve this question

7 Answers

up vote 27 down vote accepted

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. There's a complete list in the Apache Commons, though: org.apache.commons.HttpClient.HttpStatus.

share|improve this answer
There's is no such a thing as "complete list", as status codes can be and do get extended. – Julian Reschke Mar 22 '12 at 7:14
2  
@JulianReschke I think "complete" here should be taken to mean "conforms to all the codes outlined by the standard". – John Feminella Mar 22 '12 at 15:19
John: "the standard" does not define "all" the status codes. That's why there is a registry. – Julian Reschke Mar 23 '12 at 20:58
iana.org/assignments/http-status-codes/http-status-codes.xml have a big list including the code=100 – Garis M Suero Mar 27 at 19:11

The Interface javax.servlet.http.HttpServletResponse from the servlet API has all the response codes in the for of int constants names SC_<description>. See http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html

share|improve this answer
Depends on how you define "all". – Julian Reschke Apr 8 '09 at 15:39
1  
HttpServletResponse supports the RFC1945 and part of RFC2616 standards, but it's missing all of RFC2518. If you need a complete list, see HttpStatus as I mentioned. – John Feminella Apr 9 '09 at 3:57

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

share|improve this answer

Well, there are static constants of the exact integer values in the HttpURLConnection class

share|improve this answer

Also check out the Restlet Status class:

http://www.restlet.org/documentation/1.1/api/org/restlet/data/Status.html

share|improve this answer
Again; status codes are extensible, so there can't be a "complete" list, unless it's revised everytime a new status code is added to the IANA registry (iana.org/assignments/http-status-codes) – Julian Reschke Jan 26 '10 at 12:52

Use javax.servlet.http.HttpServletResponse class

Example:

javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED //401
javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR //500
share|improve this answer
for client development, it's burdensome to include the entire servlet API just to pick up these codes. – Jeffrey Blattman Jan 11 at 20:10

The best provider for http status code constants is likely to be Jetty's org.eclipse.jetty.http.HttpStatus class because:

  • 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.

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.