jax-rs/jersey_使用JAX-RS(Jersey)的HTTP状态错误消息响应中的自定义原因短语
jax-rs/jersey
在我最近的一些工作中,我收到了在發(fā)生錯(cuò)誤時(shí)在HTTP狀態(tài)響應(yīng)中生成自定義“原因短語”的請(qǐng)求,并將其傳遞給使用我們REST API的客戶端之一。 在這篇文章中,我將演示如何使用Jersey來實(shí)現(xiàn)這一目標(biāo)。
1.定義檢查的異常和異常映射器
正如您從我的文章REST API中使用Jersey進(jìn)行錯(cuò)誤處理中發(fā)現(xiàn)的那樣,我喜歡使用Jersey的ExceptionMapper功能來處理已檢查的異常 。
為了演示的目的,我定義了一個(gè)CustomReasonPhraseException :
CustomReasonPhraseException
和CustomReasonPhraseExceptionMapper來處理映射到一個(gè)響應(yīng),如果CustomReasonPhraseException發(fā)生:
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();}}提醒:當(dāng)應(yīng)用程序引發(fā)CustomReasonPhraseException ,將調(diào)用CustomReasonPhraseExceptionMapper實(shí)例的toResponse方法。
在ExceptionMapper代碼注釋第12行中:
CustomReasonPhraseExceptionStatusType
return Response.status(new CustomReasonPhraseExceptionStatusType(Status.BAD_REQUEST))在Jersey的ResponseBuilder您可以通過實(shí)現(xiàn)javax.ws.rs.core.Response.StatusType接口來定義自己的狀態(tài)類型。
2.實(shí)現(xiàn)自定義StatusType
為了使它更具擴(kuò)展性,我創(chuàng)建了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進(jìn)行擴(kuò)展,以在響應(yīng)中提供所需的自定義Reason Phrase ( 例如“自定義錯(cuò)誤消息” ):
CustomReasonPhraseExceptionStatusType擴(kuò)展了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狀態(tài)響應(yīng)中測(cè)試自定義原因短語
請(qǐng)求
請(qǐng)求示例
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)響應(yīng)
瞧!
回應(yīng)范例
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自定義“原因短語”將按預(yù)期方式出現(xiàn)在響應(yīng)中。
提示:如果您真的想學(xué)習(xí)如何在Java中設(shè)計(jì)和實(shí)現(xiàn)REST API,請(qǐng)閱讀以下教程–借助Jersey和Spring在Java中進(jìn)行REST API設(shè)計(jì)和實(shí)現(xiàn)
摘要
您已在本文中看到了要標(biāo)記“特殊”錯(cuò)誤時(shí)如何在HTTP狀態(tài)響應(yīng)中創(chuàng)建自定義原因短語。 當(dāng)然,您也可以使用此機(jī)制為其他HTTP狀態(tài)定義自己的“原因短語”。 實(shí)際上,您不應(yīng)該濫用此原因短語功能,因?yàn)镠TTP 1.1 rfc2616中的內(nèi)容如下:
“ Status-Code元素是一個(gè)3位數(shù)的整數(shù)結(jié)果代碼,用于嘗試?yán)斫夂蜐M足請(qǐng)求。 這些代碼已在第10節(jié)中完全定義。原因短語旨在簡(jiǎn)要說明狀態(tài)代碼。 狀態(tài)碼供自動(dòng)機(jī)使用,原因短語供人類用戶使用。 不需要客戶檢查或顯示原因短語。” [1]
好,就是這樣。 繼續(xù)編碼并繼續(xù)共享編碼知識(shí)。
翻譯自: https://www.javacodegeeks.com/2014/10/custom-reason-phrase-in-http-status-error-message-response-with-jax-rs-jersey.html
jax-rs/jersey
總結(jié)
以上是生活随笔為你收集整理的jax-rs/jersey_使用JAX-RS(Jersey)的HTTP状态错误消息响应中的自定义原因短语的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ef ddl生成不了脚本_如何使用Hib
- 下一篇: 代码jit_但这是不可能的,或者无法发现