1

I´m trying to create a client for some Rest application. I tested the Rest application with Advanced REST client in Chrome.

I received:

 {
  "authorization": false,
  "provider": "Provider"
 }

That is okey. but I would like obtain this in my client:

    public class MainApp extends Application {
    public static final String BASE_URI = "http://localhost:8000/test";
    public static final String PATH_NAME= "/sytem/Consumer/r/Provider";

@Override
public void start(Stage stage) throws Exception {
}

public static void main(String[] args) {

   Client client = ClientBuilder.newClient();
    WebTarget target = client.target(BASE_URI).path(PATH_NAME);
    Response response = target.request(MediaType.APPLICATION_JSON).get();
    System.out.println(response);
    client.close();

}

}

I only receive this in the terminal:

λ java -jar Client_consumer-1.0-SNAPSHOT.jar
org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine$1@6591f517

The class is :

@XmlRootElement
public class Auth_response implements Serializable {
private String Provider;
private boolean authorization;

public Auth_response() {
     super();
}

public Auth_response(String Provider, boolean authorization) {
    super();
    this.Provider = Provider;
    this.authorization = authorization;
}

public String getProvider() {
    return Provider;
}

public boolean isAuthorization() {
    return authorization;
}

public void setProvider(String Provider) {
    this.Provider = Provider;
}

public void setAuthorization(boolean authorization) {
    this.authorization = authorization;
}

@Override
public String toString() {
    return "Auth_response{" + "Provider=" + Provider + ", authorization=" + authorization + '}';
}

}

I would like to know how to print properly the response, and how to convert to a java object again. I tried with some examples, but nothing works and I think that I need some guide.

EDIT: I tried this for getting the object:

 Auth_response r=
 client.target("http://localhost:8000/test/system/Consumer/r/Provider")
.request(MediaType.APPLICATION_JSON_TYPE).
get(Auth_response.class);
     System.out.println("funciona:");
     System.out.println(r.getProvider());
     System.out.println(r.isAuthorization());

But I obtain the next error:

Caused by: javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class ah.client_consumer.Auth_response
    at org.jboss.resteasy.core.interception.ClientReaderInterceptorContext.throwReaderNotFound(ClientReaderInterceptorContext.java:42)
    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:75)
    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:52)
    at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.aroundReadFrom(GZIPDecodingInterceptor.java:59)
    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:55)
    at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:251)
    at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:181)
    at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:213)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:105)
    ... 14 more

Exception running application ah.client_consumer.MainApp

  • The error `Caused by: javax.ws.rs.NotFoundException: HTTP 404 Not Found` means the webservice you are trying to access is not accessible. Can you please post that part as well? – Nishesh Pratap Singh Sep 26 '17 at 14:31
  • When I use the tool Advanced Client Rest the webservice works,. – cristina paniagua Sep 26 '17 at 14:42
  • I had a mistake in the path, I edited the error for the current error. – cristina paniagua Sep 26 '17 at 14:51
  • The new error, could be because of missing dependency as your **Auth_response** is not getting converted to JSON for response. If you are using jersey then try to add Jackson library and if your using rest easy then you need to add resteasy-jackson2-provider – Nishesh Pratap Singh Sep 26 '17 at 15:16

2 Answers2

0

Try changing below line :

Response response = target.request(MediaType.APPLICATION_JSON).get();
System.out.println(response);

To

Response response = target.request(MediaType.APPLICATION_JSON).get(String.class);
System.out.println(response.toString());

Because you have not specified what type of response you are accepting, you are getting object as response.

Nishesh Pratap Singh
  • 2,131
  • 2
  • 13
  • 20
0

SOLUTION:

Add to the code:

ResteasyProviderFactory instance=ResteasyProviderFactory.getInstance();
RegisterBuiltin.register(instance);
instance.registerProvider(ResteasyJacksonProvider.class);

The solution was finding in : Unable to find a MessageBodyReader of content-type application/json and type class java.lang.String