在我的应用程序中,我有 2 个项目(服务),我想从一个服务到另一个服务进行 API 调用。所以我遵循了 Quarkus Restclient 的 Quarkus 教程。但是当我打电话时,restclient 返回一个默认模型。
这是我的响应类:
public class Response {
private int status;
private String statusVerbose;
private Object data;
//Getters
public int getStatus() {return this.status;}
public String getStatusVerbose() {return this.statusVerbose;}
public Object getData() {return this.data;}
public Response(){
this.SetStatusCode(404);
this.data = new JSONObject().put("error", "Not Found");
}
public Response(int statusCode){
this.SetStatusCode(statusCode);
}
public Response(int status, Object data){
this.SetStatusCode(status);
this.SetData(data);
}
public Response(int status, Object data, String statusVerbose){
this.SetStatusCode(status);
this.SetData(data);
this.SetStatusVerbose(statusVerbose);
}
public Response(int status, String statusVerbose){
this.SetStatusCode(status);
this.SetStatusVerbose(statusVerbose);
}
public void SetStatusCode(int status) {
this.status = status;
switch(status){
case 200:
statusVerbose = "OK";
break;
case 400:
statusVerbose = "BAD_REQUEST";
break;
case 404:
statusVerbose = "NOT_FOUND";
break;
case 500:
statusVerbose = "INTERNAL_SERVER_ERROR";
break;
case 401:
statusVerbose = "NOT_AUTHORIZED";
break;
case 403:
statusVerbose = "NOT_ALLOWED";
break;
}
}
public void SetStatusVerbose(String verbose){
statusVerbose = verbose;
}
public void SetData(Object data){
this.data = data;
}
}
这是它返回的响应模型,我也让 RestClient 接收这个模型。但是 RestClient 给了我一个带有默认构造函数的 Response 对象。而是用具体的数据。
public Response IsUserASupermarket(){
Response r = new Response(200);
JSONObject obj = new JSONObject();
obj.put("isSupermarket", true);
r.SetData(obj);
System.out.println(r.getData());
return r;
}
我接到这个电话的界面:
@RegisterRestClient
public interface PortalAccountClient {
@GET
@Path("/portal/isSupermarket")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
Response IsUserASupermarket();
}
所以我得到了一个默认的构造函数 Response obj。
亲切的问候,巴特