问题描述
我有一个 API,当我通过邮递员调用它时,它会在以下情况下给出以下响应:
案例 1:状态代码:200
{"success": "student record is present","error": null}
案例 2:状态码:422
{"success": null,"error": "studentname should not contain numerics"}
我想通过 microprofile restclient 使用 quarkus/java 项目实现上述两种情况相同的结果。所以创建了以下类
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 也为空。我想在带有 microprofile rest 客户端案例的 422 状态代码案例中获取错误消息(json 响应)。 有什么方法/建议可以实现这一目标?
解决方法
不幸的是,这不是 Microprofile 客户端支持的内容。 然而,这听起来像是一个有用的功能,所以您介意在 Quarkus 问题跟踪器中打开一个问题吗?