北京价格微网站建设,同安网站建设,网站策划ps,网页小游戏链接在最近的一些工作中#xff0c;我收到了在发生错误时在HTTP状态响应中生成自定义“原因短语”的请求#xff0c;该状态短语传递给我们的一个REST API消耗客户端。 在这篇文章中#xff0c;我将演示如何使用Jersey来实现。 1.定义检查的异常和异常映射器 正如您可能从我的文… 在最近的一些工作中我收到了在发生错误时在HTTP状态响应中生成自定义“原因短语”的请求该状态短语传递给我们的一个REST API消耗客户端。 在这篇文章中我将演示如何使用Jersey来实现。 1.定义检查的异常和异常映射器 正如您可能从我的文章使用Jersey的REST API中的错误处理中发现的那样我喜欢使用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 ExceptionMapperCustomReasonPhraseException {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