0

In my application I have 2 projects (services), I want to make an API call from one service to another. So I followed the Quarkus tutorial for Quarkus Restclient. But when I make the call the restclient returns a default model.

This is my Response class:

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;
}
}

This is the Response model that it being returned, I also have the RestClient receive this model. But RestClient gives me a Response object with the default constructor. Instead with the specific data.

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;
    }

The interface where I receive this call:

@RegisterRestClient
public interface PortalAccountClient {

    @GET
    @Path("/portal/isSupermarket")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    Response IsUserASupermarket();
}

So I am getting an default constructor Response obj back.

Kind Regards, Bart

  • Hi Bart, could you share your code in here, or giving us the link to the repository? Becasue `Response` might be imported from other package. Are you sure you are sharing this response class in both projects? A last, unrelated, remark: in Java you name a method starting from lowercase, it should be `isUserASupermarket()` – Davide Martorana Nov 04 '20 at 17:57

2 Answers2

0

I suspect your REST Client interface is actually using the Response class from JAX-RS.

Can you verify the imports on the interface to ensure it's the Response class you created and not the one from JAX-RS?

Ken
  • 835
  • 4
  • 11
0

First of all I want to thank @Ken and @davide79 for their help. I check at it was a rookie mistake :(.

My Response object was the right one, but they had the wrong getters and setters (wrong casing) so RestClient didnt detect them as getters and setters.

So, Ken and Davide79 thanks.

This:

public Object GetData(){return this.data;}

has to be:

public Object getData(){return this.data;}
  • Bart
Sven Hakvoort
  • 3,543
  • 2
  • 17
  • 34