jax-rs/jersey
在我最近的一些工作中,我收到了在发生错误时在HTTP状态响应中生成自定义“原因短语”的请求,并将其传递给使用我们REST API的客户端之一。 在这篇文章中,我将演示如何使用Jersey来实现这一目标。
1.定义检查的异常和异常映射器
正如您从我的文章REST API中使用Jersey进行错误处理中发现的那样,我喜欢使用Jersey的ExceptionMapper功能来处理已检查的异常 。
为了演示的目的,我定义了一个CustomReasonPhraseException
:
CustomReasonPhraseException
package org.codingpedia.demo.rest.errorhandling;public class CustomReasonPhraseException extends Exception {private static final long serialVersionUID = -271582074543512905L;private final int businessCode;public CustomReasonPhraseException(int businessCode, String message) {super(message);this.businessCode = businessCode;}public int getBusinessCode() {return businessCode;}}
和CustomReasonPhraseExceptionMapper
来处理映射到一个响应,如果CustomReasonPhraseException
发生:
CustomReasonPhraseExceptionMapper
package org.codingpedia.demo.rest.errorhandling;import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;@Provider
public class CustomReasonPhraseExceptionMapper implements ExceptionMapper<CustomReasonPhraseException> {public Response toResponse(CustomReasonPhraseException bex) {return Response.status(new CustomReasonPhraseExceptionStatusType(Status.BAD_REQUEST)).entity("Custom Reason Phrase exception occured : " + bex.getMessage()).build();}}
提醒:当应用程序引发CustomReasonPhraseException
,将调用CustomReasonPhraseExceptionMapper
实例的toResponse
方法。
在ExceptionMapper
代码注释第12行中:
CustomReasonPhraseExceptionStatusType
return Response.status(new CustomReasonPhraseExceptionStatusType(Status.BAD_REQUEST))
在Jersey的ResponseBuilder
您可以通过实现javax.ws.rs.core.Response.StatusType
接口来定义自己的状态类型。
2.实现自定义StatusType
为了使它更具扩展性,我创建了AbstractStatusType
类:
AbstractStatusType
package org.codingpedia.demo.rest.errorhandling;import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.Response.Status.Family;
import javax.ws.rs.core.Response.StatusType;/*** Class used to provide custom StatusTypes, especially for the the Reason Phrase that appears in the HTTP Status Response*/
public abstract class AbstractStatusType implements StatusType {public AbstractStatusType(final Family family, final int statusCode,final String reasonPhrase) {super();this.family = family;this.statusCode = statusCode;this.reasonPhrase = reasonPhrase;}protected AbstractStatusType(final Status status,final String reasonPhrase) {this(status.getFamily(), status.getStatusCode(), reasonPhrase);}@Overridepublic Family getFamily() { return family; }@Overridepublic String getReasonPhrase() { return reasonPhrase; }@Overridepublic int getStatusCode() { return statusCode; }private final Family family;private final int statusCode;private final String reasonPhrase;}
之后,我使用CustomReasonPhraseExceptionStatusType
进行扩展,以在响应中提供所需的自定义Reason Phrase
( 例如“自定义错误消息” ):
CustomReasonPhraseExceptionStatusType扩展了AbstractStatusType
package org.codingpedia.demo.rest.errorhandling;import javax.ws.rs.core.Response.Status;/*** Implementation of StatusType for CustomReasonPhraseException.* The Reason Phrase is set in this case to "Custom error message"*/
public class CustomReasonPhraseExceptionStatusType extends AbstractStatusType{private static final String CUSTOM_EXCEPTION_REASON_PHRASE = "Custom error message";public CustomReasonPhraseExceptionStatusType(Status httpStatus) {super(httpStatus, CUSTOM_EXCEPTION_REASON_PHRASE);}}
3.在HTTP状态响应中测试自定义原因短语
请求
请求示例
GET http://localhost:8888/demo-rest-jersey-spring/mocked-custom-reason-phrase-exception HTTP/1.1
Accept-Encoding: gzip,deflate
Host: localhost:8888
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
响应
瞧!
回应范例
HTTP/1.1 400 Custom error message
Content-Type: text/plain
Content-Length: 95
Server: Jetty(9.0.7.v20131107)Custom Reason Phrase exception occured : message attached to the Custom Reason Phrase Exception
自定义“原因短语”将按预期方式出现在响应中。
提示:如果您真的想学习如何在Java中设计和实现REST API,请阅读以下教程–借助Jersey和Spring在Java中进行REST API设计和实现
摘要
您已在本文中看到了要标记“特殊”错误时如何在HTTP状态响应中创建自定义原因短语。 当然,您也可以使用此机制为其他HTTP状态定义自己的“原因短语”。 实际上,您不应该滥用此原因短语功能,因为HTTP 1.1 rfc2616中的内容如下:
“ Status-Code元素是一个3位数的整数结果代码,用于尝试理解和满足请求。 这些代码已在第10节中完全定义。原因短语旨在简要说明状态代码。 状态码供自动机使用,原因短语供人类用户使用。 不需要客户检查或显示原因短语。” [1]
好,就是这样。 继续编码并继续共享编码知识。
翻译自: https://www.javacodegeeks.com/2014/10/custom-reason-phrase-in-http-status-error-message-response-with-jax-rs-jersey.html
jax-rs/jersey