簡體   English   中英

Quarkus Microprofile Rest 客戶端 ResponseExceptionMapper 未捕獲錯誤

[英]Quarkus Microprofile Rest Client ResponseExceptionMapper doesn't catch errors

我在 Quarkus (1.8.1) 服務中有一個 Rest 客戶端,定義如下:

@RegisterRestClient
@Path("/")
@Produces("application/json")
@Consumes("application/json")
public interface MyClient {
    @POST
    @Path("/{entity}")
    Response postEntity(@HeaderParam(value = "Authorization") String auth, 
            @PathParam("entity") String entity, Object payload) throws MyException;
}

我已經在同一個 package 中實現了ResponseExceptionMapper ,如下所示:

public class MyExceptionMapper implements ResponseExceptionMapper<MyException> {
    @Override
    public MyException toThrowable(Response r) {
        return new DynamicsException(r.getStatus() + " - " + r.readEntity(String.class));
    }
}

當我調用該服務時,它當前返回 404 錯誤,我希望調用MyExceptionMapper class 中的代碼。 但是它沒有,而是拋出了javax.ws.rs.WebApplicationException 堆棧跟蹤包括對DefaultResponseExceptionMapper的調用。 看來我的映射器尚未注冊。

如何注冊我的處理程序以獲取來自服務調用的無效響應?

您需要使用@RegisterProvider(MyExceptionMapper.class)將 MyExceptionMapper 注冊為其余客戶端的提供@RegisterProvider(MyExceptionMapper.class)

@RegisterRestClient
@RegisterProvider(MyExceptionMapper.class)
@Path("/")
@Produces("application/json")
@Consumes("application/json")
public interface MyClient {

每個實現都提供一個默認的ResponseExceptionMapper實現,它將 map 並在響應狀態代碼 >= 400 時調用對 javax.ws.rs.WebApplicationException 的響應。它的優先級為 Integer.MAX_VALUE,旨在用作后備每當遇到錯誤。 默認情況下,此映射器將注冊到所有客戶端接口,但可以通過將 MP Config 屬性 microprofile.rest.client.disable.default.mapper 設置為 true 來禁用此映射器。

RestClientBuilder.newBuilder().property("microprofile.rest.client.disable.default.mapper",true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM