Quarkus Microprofile Rest Client ResponseExceptionMapper无法捕获错误

huangapple go评论106阅读模式
英文:

Quarkus Microprofile Rest Client ResponseExceptionMapper doesn't catch errors

问题

我在一个 Quarkus (1.8.1) 服务中有一个 Rest Client,定义如下:

@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;
}

我在同一个包中实现了 ResponseExceptionMapper,代码如下:

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

当我调用服务时,它当前返回一个 404 错误,我预期会调用 MyExceptionMapper 类中的代码。然而事实并非如此,而是抛出了一个 javax.ws.rs.WebApplicationException 异常。堆栈跟踪中包括对 DefaultResponseExceptionMapper 的调用。似乎我的映射器未被注册。

我该如何注册用于处理调用服务时无效响应的处理程序?

英文:

I have a Rest Client in a Quarkus (1.8.1) service defined like this:

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

And I have implemented ResponseExceptionMapper in the same package like this:

public class MyExceptionMapper implements ResponseExceptionMapper&lt;MyException&gt; {
    @Override
    public MyException toThrowable(Response r) {
        return new DynamicsException(r.getStatus() + &quot; - &quot; + 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. However it doesn't and instead a javax.ws.rs.WebApplicationException is thrown. The stack trace includes a call to the 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?

答案1

得分: 7

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

You need to register MyExceptionMapper as provider to the rest client with @RegisterProvider(MyExceptionMapper.class).

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

答案2

得分: 2

每个实现都提供了一个默认的ResponseExceptionMapper实现,当响应状态码大于等于400时,它将映射并调用javax.ws.rs.WebApplicationException的响应。它的优先级为Integer.MAX_VALUE,意味着在遇到错误时应作为备用使用。这个映射器将默认注册到所有客户端接口,但可以通过设置一个MP配置属性microprofile.rest.client.disable.default.mapper为true来禁用它。

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

英文:

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. 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.

RestClientBuilder.newBuilder().property(&quot;microprofile.rest.client.disable.default.mapper&quot;,true)

答案3

得分: 0

为您提供默认实现:

@ClientExceptionMapper
static RuntimeException toException(Response response) {
    if (response.getStatus() > 200) {
        return new RuntimeException(response.readEntity(String.class));
    }
    return null;
}
英文:

It provides default implementation:

@ClientExceptionMapper
	static RuntimeException toException(Response response) {
		if (response.getStatus() &gt; 200) {
			return new RuntimeException(response.readEntity(String.class));
		}
		return null;
	}


</details>



huangapple
  • 本文由 发表于 2020年10月6日 01:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64213483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定