Java library to map HTTP status code to descriptio

2019-04-20 12:49发布

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.

7条回答
Explosion°爆炸
2楼-- · 2019-04-20 13:12

You can use my HttpStatus JSP Tag Libray:

<%@ page isErrorPage="true" %>
<%@ taglib prefix="hs" uri="http://erik.thauvin.net/taglibs/httpstatus" %>
<html><head>
<title><hs:code/> <hs:reason default="Server Error"/></title>
</head>
<h1><hs:reason default="Server Error"/></h1>
Cause: <pre><hs:cause default="Unable to complete your request."/></pre>  

Specifically, as shown in the example above, the <hs:reason/> tag is used to display the reason phrase (human-readable description) for the current HTTP status code in the page's title and first heading.

Additionally, the <hs:code/> tag is used to display the current HTTP status code and the <hs:cause/> tag to display the message from the exception that caused the error.

查看更多
Lonely孤独者°
3楼-- · 2019-04-20 13:13

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
查看更多
Melony?
4楼-- · 2019-04-20 13:17

If you're using Jersey (or have it on your classpath), the javax.ws.rs.core.Response.Status enum includes human-readable "reasons" from the HTTP spec, e.g. Response.Status.fromStatusCode(404).toString() gives "Not Found".

查看更多
孤傲高冷的网名
5楼-- · 2019-04-20 13:21

Maybe the easiest solution is to use next function:

httpResponse.getStatusLine().getReasonPhrase()

It returns exactly what's needed.

查看更多
够拽才男人
6楼-- · 2019-04-20 13:22

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.

查看更多
登录 后发表回答