Java library to map HTTP status code to description?

21,968

Solution 1

So I'm wondering if there is a Java library that provides a good mapping between HTTP status codes and a brief human-readable description of them (ideally a 2-4 word "summary", for use as a page title, as well as a 1-3 sentence message expanding on the summary).

Yes, Apache Commons HttpClient has this functionality. The HttpStatus class has the same list of int constants that you'll find elsewhere, but it also has a static String getStatusText(int) method that returns a human-readable description of the status code.

Here is the Maven dependency:

<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>

Example code:

import org.apache.commons.httpclient.HttpStatus;

String responseMessage = HttpStatus.getStatusText(HttpStatus.SC_OK);
System.out.println(responseMessage);

Prints:

OK

Solution 2

I think you might like the Spring solution: http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/http/HttpStatus.html

Solution 3

Latest apache http components v4+ ( httpCoponents )

Introduces the following: HttpStatus as an enum containing all of the httpStatuses

For their description use: EnglishReasonPhraseCatalog.INSTANCE.getReason(code, Locale.ENGLISH);

Where your status code == code, eg: 200

For example:

HttpStatus.SC_FORBIDDEN == 403
EnglishReasonPhraseCatalog.INSTANCE.getReason(403, Locale.ENGLISH) == "Forbidden"

Solution 4

Maybe the easiest solution is to use next function:

httpResponse.getStatusLine().getReasonPhrase()

It returns exactly what's needed.

Solution 5

Abstract class 'HttpURLConnection' provides you with constant int values for all HTTP status codes. Its documentation has a short verbal description for each constant. You could make yourself a simple enum with these values and strings, and use that.

Share:
21,968
Andrzej Doyle
Author by

Andrzej Doyle

Chief Technology Officer at EDA Family Solutions

Updated on November 19, 2020

Comments

  • Andrzej Doyle
    Andrzej Doyle over 3 years

    I'm in the situation where I'm writing custom error pages for a webapp (primarily to reduce information disclosure from the servlet container's default error pages). Since I need an error page for each error status code, I'm going to have to have a sensible response for each code. As far as I can tell, these error pages don't have to be particularly user-friendly, but simply redirecting everything to a single "it went wrong" error page is going to make diagnosing problems very difficult.

    So I'm wondering if there is a Java library that provides a good mapping between HTTP status codes and a brief human-readable description of them (ideally a 2-4 word "summary", for use as a page title, as well as a 1-3 sentence message expanding on the summary). Then I could just use this in a JSP to provide some feedback on the class of the error. If not I'm sure I can write one myself, but if wheels have been invented I'm happy to use them.