将 HTTP 状态代码映射到描述的 Java 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1637750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java library to map HTTP status code to description?
提问by Andrzej Doyle
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 needan 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.
我正在为 webapp 编写自定义错误页面(主要是为了减少来自 servlet 容器的默认错误页面的信息泄露)。因为我需要每个错误状态代码的错误页面,所以我必须对每个代码做出合理的响应。据我所知,这些错误页面不必对用户特别友好,但简单地将所有内容重定向到一个“出错了”的错误页面会使诊断问题变得非常困难。
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.
所以我想知道是否有一个 Java 库可以在 HTTP 状态代码和它们的简短人类可读描述之间提供良好的映射(理想情况下是一个 2-4 个单词的“摘要”,用作页面标题,以及扩展摘要的 1-3 句消息)。然后我可以在 JSP 中使用它来提供有关错误类别的一些反馈。如果没有,我确定我可以自己写一个,但如果轮子已经发明了,我很乐意使用它们。
回答by skaffman
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).
所以我想知道是否有一个 Java 库可以在 HTTP 状态代码和它们的简短人类可读描述之间提供良好的映射(理想情况下是一个 2-4 个单词的“摘要”,用作页面标题,以及扩展摘要的 1-3 句消息)。
Yes, Apache Commons HttpClienthas this functionality. The HttpStatusclass has the same list of intconstants 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.
是的,Apache Commons HttpClient具有此功能。该HttpStatus班有相同的列表int常量,你会发现其他地方,但它也有一个static String getStatusText(int)返回的状态代码的可读性的描述方法。
Here is the Mavendependency:
这是Maven依赖项:
<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
回答by Ed J
I think you might like the Spring solution: http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/http/HttpStatus.html
我想你可能喜欢 Spring 解决方案:http: //static.springsource.org/spring/docs/3.0.x/api/org/springframework/http/HttpStatus.html
回答by Vitali Olshevski
Maybe the easiest solution is to use next function:
也许最简单的解决方案是使用下一个功能:
httpResponse.getStatusLine().getReasonPhrase()
It returns exactly what's needed.
它准确地返回所需的内容。
回答by Chaiavi
Latest apache http components v4+ ( httpCoponents )
最新的 apache http 组件 v4+ ( httpCoponents )
Introduces the following: HttpStatus as an enum containing all of the httpStatuses
引入以下内容: HttpStatus 作为包含所有 httpStatuses 的枚举
For their description use: EnglishReasonPhraseCatalog.INSTANCE.getReason(code, Locale.ENGLISH);
对于他们的描述使用:EnglishReasonPhraseCatalog.INSTANCE.getReason(code, Locale.ENGLISH);
Where your status code == code, eg: 200
您的状态代码 == 代码的位置,例如:200
For example:
例如:
HttpStatus.SC_FORBIDDEN == 403
EnglishReasonPhraseCatalog.INSTANCE.getReason(403, Locale.ENGLISH) == "Forbidden"
回答by Yuval
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.
抽象类“ HttpURLConnection”为您提供所有 HTTP 状态代码的常量 int 值。它的文档对每个常量都有简短的口头描述。您可以使用这些值和字符串使自己成为一个简单的枚举,然后使用它。
回答by DanC
If you're using Jersey (or have it on your classpath), the javax.ws.rs.core.Response.Statusenum includes human-readable "reasons" from the HTTP spec, e.g. Response.Status.fromStatusCode(404).toString()gives "Not Found".
如果您正在使用 Jersey(或在您的类路径中使用它),则javax.ws.rs.core.Response.Status枚举包括来自 HTTP 规范的人类可读的“原因”,例如Response.Status.fromStatusCode(404).toString()给出"Not Found".
回答by Erik C. Thauvin
You can use my HttpStatus JSP Tag Libray:
您可以使用我的HttpStatus JSP 标签库:
<%@ 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.
具体来说,如上例所示,该<hs:reason/>标签用于在页面的标题和第一个标题中显示当前 HTTP 状态代码的原因短语(人类可读的描述)。
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.
此外,该<hs:code/>标签用于显示当前的 HTTP 状态代码和<hs:cause/>用于显示来自导致错误的异常的消息的标签。

