【问题标题】:Resteasy error response body is unreachableResteasy 错误响应正文无法访问
【发布时间】:2021-10-20 00:20:07
【问题描述】:

我正在使用 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 读取实体,但实体始终为空。在 Postman 服务中,返回正文如下:

{
   "error" : {
      "id" : "id does not exist"
      }
}

【问题讨论】:

    标签: java resteasy quarkus


    【解决方案1】:

    查看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);
             ............
            }
        }
    }
    

    或者

        public class CustomExceptionMapper
                implements ResponseExceptionMapper<Throwable> {
    }
    

    注册 ResponseExceptionMapper 提供者:

    @Path("/xyz")
    @RegisterProvider(CustomResponseExceptionMapper.class)
    @RegisterRestClient
    @Timeout
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-14
      • 2019-09-23
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多