简体   繁体   English

Quarkus Microprofile Rest 客户端 ResponseExceptionMapper 未捕获错误

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

I have a Rest Client in a Quarkus (1.8.1) service defined like this:我在 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;
}

And I have implemented ResponseExceptionMapper in the same package like this:我已经在同一个 package 中实现了ResponseExceptionMapper ,如下所示:

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

When I call the service it is currently returning a 404 error, and I expected the code in the MyExceptionMapper class to be called.当我调用该服务时,它当前返回 404 错误,我希望调用MyExceptionMapper class 中的代码。 However it doesn't and instead a javax.ws.rs.WebApplicationException is thrown.但是它没有,而是抛出了javax.ws.rs.WebApplicationException The stack trace includes a call to the DefaultResponseExceptionMapper .堆栈跟踪包括对DefaultResponseExceptionMapper的调用。 It's seems that my mapper has not been registered.看来我的映射器尚未注册。

How can I register my handler for invalid responses from calls to the service?如何注册我的处理程序以获取来自服务调用的无效响应?

You need to register MyExceptionMapper as provider to the rest client with @RegisterProvider(MyExceptionMapper.class) .您需要使用@RegisterProvider(MyExceptionMapper.class)将 MyExceptionMapper 注册为其余客户端的提供@RegisterProvider(MyExceptionMapper.class)

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

Each implementation provides a default ResponseExceptionMapper implementation that will map and invoke a response to javax.ws.rs.WebApplicationException when the response status code is >= 400. It has a priority of Integer.MAX_VALUE, and is meant to be used as a fallback whenever an error is encountered.每个实现都提供一个默认的ResponseExceptionMapper实现,它将 map 并在响应状态代码 >= 400 时调用对 javax.ws.rs.WebApplicationException 的响应。它的优先级为 Integer.MAX_VALUE,旨在用作后备每当遇到错误。 This mapper will be registered bydefault to all client interfaces, but this can be disabled by setting an MP Config property, microprofile.rest.client.disable.default.mapper, to true.默认情况下,此映射器将注册到所有客户端接口,但可以通过将 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