问题描述
在我的应用程序中,我有2个项目(服务),我想从一个服务到另一个服务进行API调用。 因此,我遵循了Quarkus Restclient的Quarkus教程。但是当我打电话时,restclient返回一个默认模型。
这是我的Response类:
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;
}
}
这是返回的Response模型,我也让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();
}
亲切的问候, 巴特
解决方法
我怀疑您的REST客户端界面实际上正在使用JAX-RS中的Response
类。
您是否可以在接口上验证导入,以确保它是您创建的Response
类,而不是JAX-RS中的那个?
首先,我要感谢@Ken和@ davide79的帮助。我检查这是菜鸟的错误:(。
我的Response对象是正确的对象,但是它们的getter和setters错误(大小写错误),因此RestClient不会将它们检测为getter和setters。
所以Ken和Davide79谢谢。
此:
public Object GetData(){return this.data;}
必须是:
public Object getData(){return this.data}
- 巴特