How can I convert HTTP status code to its text representation, in Java? I mean are there any existing implementations of such a conversion. The best I've found so far is java.ws.rs.core.Response.Status#fromStatusCode(), which converts only a limited subset of all statuses.
-
Create your own map <Integer, String>. You can read it from an xml, for example.StKiller– StKiller2011-11-18 08:24:27 +00:00Commented Nov 18, 2011 at 8:24
-
2I'm pretty sure that a conformant HTTP server MUST send a status code AND a status text.Piskvor left the building– Piskvor left the building2011-11-18 08:34:04 +00:00Commented Nov 18, 2011 at 8:34
-
2Yes, I also think that my server has to send code and text, that's why I'm asking :)yegor256– yegor2562011-11-18 09:11:12 +00:00Commented Nov 18, 2011 at 9:11
Add a comment
|
3 Answers
If you're happy to import Spring web, org.springframework.http.HttpStatus.valueOf(int).name() should do, if you don't mind underscores.
2 Comments
pablomolnar
This returns the code number as string, not the text representation.
Prasannjeet Singh
org.springframework.http.HttpStatus.valueOf(response.statusCode()).getReasonPhrase() will workApache HttpComponents has an (old-style) enum class which does this:
http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpStatus.html
You can call itsgetStatusText method with an enum instance as the argument to get the text representation of a status code.
Maven dependency is:
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
1 Comment
uwe
The "commons-httpclient" is end of life (last release 2007). The new library is org.apache.httpcomponents:httpcore and the method to get the status text is EnglishReasonPhraseCatalog.INSTANCE.getReason(status, null);