Reputation: 105213
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.
Upvotes: 13
Views: 20221
Reputation: 1457
If you are working with Spring, or are okay with importing Spring Web, then this would do it:
First import it:
import org.springframework.http.HttpStatus;
Then use it like:
int statusCode = 502;
String message = HttpStatus.valueOf(statusCode).getReasonPhrase();
Upvotes: 2
Reputation: 32437
If you're happy to import Spring web, org.springframework.http.HttpStatus.valueOf(int).name()
should do, if you don't mind underscores.
Upvotes: 12
Reputation: 438
Apache 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>
Upvotes: 6