日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用JAX-RS(Jersey)的HTTP状态错误消息响应中的自定义原因短语

發(fā)布時間:2023/12/3 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用JAX-RS(Jersey)的HTTP状态错误消息响应中的自定义原因短语 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在最近的一些工作中,我收到了在發(fā)生錯誤時在HTTP狀態(tài)響應中生成自定義“原因短語”的請求,該狀態(tài)短語傳遞給我們的一個REST API消耗客戶端。 在這篇文章中,我將演示如何使用Jersey來實現(xiàn)。

1.定義檢查的異常和異常映射器

正如您可能從我的文章使用Jersey的REST API中的錯誤處理中發(fā)現(xiàn)的那樣,我喜歡使用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發(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();}}

提醒:當應用程序引發(fā)CustomReasonPhraseException ,將調(diào)用CustomReasonPhraseExceptionMapper實例的toResponse方法。

在ExceptionMapper代碼注釋第12行中:

CustomReasonPhraseExceptionStatusType

return Response.status(new CustomReasonPhraseExceptionStatusType(Status.BAD_REQUEST))

在Jersey的ResponseBuilder您可以通過實現(xiàn)javax.ws.rs.core.Response.StatusType接口來定義自己的狀態(tài)類型。

2.實現(xiàn)自定義StatusType

為了使它更具擴展性,我創(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進行擴展,以在響應中提供我想要的自定義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狀態(tài)響應中測試自定義原因短語

請求

要求范例

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

自定義“原因短語”將按預期方式出現(xiàn)在響應中。

提示:如果您真的想學習如何在Java中設計和實現(xiàn)REST API,請閱讀以下教程–借助Jersey和Spring在Java中進行REST API設計和實現(xiàn)

摘要

您已在本文中看到了要標記“特殊”錯誤時如何在HTTP狀態(tài)響應中創(chuàng)建自定義原因短語。 當然,您也可以使用此機制為其他HTTP狀態(tài)定義自己的“原因短語”。 實際上,您不應該濫用此原因短語功能,因為HTTP 1.1 rfc2616中的內(nèi)容如下:

“ Status-Code元素是3位數(shù)的整數(shù)結(jié)果代碼,用于嘗試理解和滿足請求。 這些代碼在第10節(jié)中有完整定義。原因短語旨在簡要說明狀態(tài)代碼。 狀態(tài)碼供自動機使用,原因短語供人類用戶使用。 不需要客戶檢查或顯示原因短語。” [1]

好吧,就是這樣。 繼續(xù)編碼并繼續(xù)共享編碼知識。

翻譯自: https://www.javacodegeeks.com/2014/10/custom-reason-phrase-in-http-status-error-message-response-with-jax-rs-jersey.html

總結(jié)

以上是生活随笔為你收集整理的使用JAX-RS(Jersey)的HTTP状态错误消息响应中的自定义原因短语的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。