我正在使用 Quarkus 框架和 RestEasy 进行 REST 通信。
当响应代码为 200 等时,一切正常。当客户端收到错误代码时,例如400 Bad Request Resteasy 返回 WebApplicationException,我无法到达响应正文。
MyService.java
@Path("/")
@RegisterRestClient
@RegisterProvider(value = ClientLoggingFilter.class, priority = 100)
public interface MyService {
@POST
@Path("/foo")
@Consumes(MediaType.APPLICATION_JSON)
MyRespnse create(MyRequest request);
我一直在尝试从 WebApplicationException 读取实体,但实体始终为空。在邮递员服务中返回正文如下:
{
"error" : {
"id" : "id does not exist"
}
}
调查http://download.eclipse.org/microprofile/microprofile-rest-client-1.0/apidocs/org/eclipse/microprofile/rest/client/ext/ResponseExceptionMapper.html http://download.eclipse.org/microprofile/microprofile-rest-client-1.0/apidocs/org/eclipse/microprofile/rest/client/ext/ResponseExceptionMapper.html
@Provider
public class CustomResponseExceptionMapper implements ResponseExceptionMapper<RuntimeException> {
public CustomResponseExceptionMapper () {
}
public boolean handles(int statusCode, MultivaluedMap<String, Object> headers) {
}
public CusomExceptionMapper toThrowable(Response response) {
try {
String responseString = (String)response.readEntity(String.class);
............
}
}
}
Or
public class CustomExceptionMapper
implements ResponseExceptionMapper<Throwable> {
}
注册 ResponseExceptionMapper 提供程序:
@Path("/xyz")
@RegisterProvider(CustomResponseExceptionMapper.class)
@RegisterRestClient
@Timeout
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)