简体   繁体   English

无法 map / 在 Microprofile Rest 客户端中获取 422 错误代码的响应实体

[英]Unable to map / fetch the response entity on 422 error code in Microprofile Rest Client

I have an API, when I call it through postman it gives the below response on the following cases:我有一个 API,当我通过 postman 调用它时,它会在以下情况下给出以下响应:

Case1: status code: 200 Case1:状态码:200

{"success": "student record is present",
  "error": null}

Case2: status code: 422 Case2:状态码:422

{"success": null,
  "error": "studentname should not contain numerics"}

I want to achieve the same above results of two case through microprofile restclient using quarkus/java project.我想通过使用 quarkus/java 项目的 microprofile restclient 实现上述两个案例的相同结果。 So created the below classes所以创建了下面的类

Java DTO Class: Java DTO Class:

public class StudentResponse{

    private String success;

    private String error;

    public String getSuccess() {
        return success;
    }

    public void setSuccess(String success) {
        this.success = success;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    @Override
    public String toString() {
        return "StudentResponse [success=" + success + ", error=" + error + "]";
    }

}

Rest-Client Class:休息客户端 Class:

package com.tatadigital.rest.service; package com.tatadigital.rest.service;

@RegisterRestClient(configKey = "student-client-api")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public interface StudentService {

    @POST
    @Path("/checkStudent")
    StudentResponse checkStudent(@RequestBody StudentCheck studentCheck);
}

Finally, I tested it through the app, and for case-1, receiving the response body with status code 200 as expected.最后,我通过应用程序对其进行了测试,对于案例 1,按预期收到状态代码为 200 的响应正文。 But for case-2 as the status code is 422 the exception is thrown and getting handled but in the exception object we have response object and inside it we have response entity.但是对于 case-2,因为状态代码是 422,异常被抛出并得到处理,但是在异常 object 中我们有响应 object 并且在它里面我们有响应实体。 This response entity is null and even studentResponse is also null. I want to get the error message(json response) in 422 status code case with microprofile rest client case.此响应实体是 null,甚至 studentResponse 也是 null。我想在 422 状态代码案例中获取错误消息(json 响应),微配置文件为 rest 客户端案例。 Any approach/suggestion to achieve this?有什么方法/建议可以实现这一目标?

Edit编辑

You can also achieve the same behavior from the DefaultResponseExceptionMapper by adding您还可以通过添加 DefaultResponseExceptionMapper 实现相同的行为
resteasy.original.webapplicationexception.behavior=true
in the application.properties.在 application.properties 中。

When this option is false , the DefaultResponseExceptionMapper wraps the WebApplicationException and returns an exception according to the HTTP status code, for example当此选项为false时,DefaultResponseExceptionMapper 包装 WebApplicationException 并根据 HTTP 状态码返回异常,例如

} else if (e instanceof BadRequestException) {
    return new ResteasyBadRequestException((BadRequestException)e);
}

which doesn't contain the entity from the response.其中不包含响应中的实体。

If you enable the original behavior, the exception is returned as is and the response entity is available for you to read it.如果启用原始行为,异常将按原样返回,并且响应实体可供您阅读。

wae.getResponse().readEntity(StudentResponse.class);


In case of a status code >=400 a WebApplicationException is thrown that holds a Response object, but is handled by the DefaultResponseExceptionMapper .在状态代码 >=400 的情况下,将抛出一个WebApplicationException ,它包含一个 Response object,但由DefaultResponseExceptionMapper处理。

Create a custom exception mapper and throw a WebApplicationException that contains only the response创建自定义异常映射器并抛出仅包含响应的 WebApplicationException

public class HttpExceptionMapper implements ResponseExceptionMapper<Throwable> {

    @Override
    public Throwable toThrowable(Response response) {
        throw new WebApplicationException(response);
    }

}

Register the mapper to your rest client with the appropriate annotation @RegisterProvider(HttpExceptionMapper.class)使用适当的注释@RegisterProvider(HttpExceptionMapper.class)将映射器注册到您的 rest 客户端

and you will be able to read the entity in the response您将能够阅读响应中的实体

StudentResponse studentResponse;
try {
  studentResponse = checkStudent(studentCheck);
} catch(WebApplicationException wae) {
  studentResponse = wae.getResponse().readEntity(StudentResponse.class);
}

Unfortunately this isn't something supported by the Microprofile client.不幸的是,这不是 Microprofile 客户端所支持的。 However this sounds like a useful feature, so do you mind opening an issue in the Quarkus issue tracker?然而,这听起来像是一个有用的功能,所以你介意在 Quarkus 问题跟踪器中打开一个问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM