无法在 Microprofile Rest Client 中映射/获取 422 错误代码上的响应实体

Posted

技术标签:

【中文标题】无法在 Microprofile Rest Client 中映射/获取 422 错误代码上的响应实体【英文标题】:Unable to map / fetch the response entity on 422 error code in Microprofile Rest Client 【发布时间】:2021-07-26 18:59:55 【问题描述】:

我有一个 API,当我通过邮递员调用它时,它会在以下情况下给出以下响应:

案例1:状态码:200

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

案例2:状态码:422

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

我想通过使用 quarkus/java 项目的 microprofile restclient 实现上述两种情况的相同结果。所以创建了以下类

Java DTO 类:

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 类:

包 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);

最后,我通过应用程序对其进行了测试,对于 case-1,按预期接收状态码为 200 的响应正文。但是对于 case-2,因为状态码是 422,所以会抛出异常并得到处理,但是在异常对象中我们有响应对象,并且在其中我们有响应实体。此响应实体为空,甚至 studentResponse 也为空。我想在 422 状态代码案例中获取错误消息(json 响应)和 microprofile 休息客户端案例。 有什么方法/建议可以实现这一目标?

【问题讨论】:

【参考方案1】:

很遗憾,Microprofile 客户端不支持此功能。 不过,这听起来像是一个有用的功能,所以您介意在 Quarkus 问题跟踪器中打开一个问题吗?

【讨论】:

以上是关于无法在 Microprofile Rest Client 中映射/获取 422 错误代码上的响应实体的主要内容,如果未能解决你的问题,请参考以下文章