3

I use java ee 7 on Glassfish 4.1.1. There is a rest service written in C# and it returns a collection of Book instances. The code on the rest client side which send get request:

public List<Bandwcorpus> downloadBandWCorpus(String corpus) {
    Client restClient = ClientBuilder.newClient();
    List<Bandwcorpus> ebooks=restClient
        .target("http://bookandwalk.pl/api/admindocumentlist")
        .queryParam("password", "XXXX").queryParam("corpusid", "YYYY")
        .request(MediaType.APPLICATION_JSON)
        .get(new GenericType<List<Book>>() {});
    restClient.close();
    return ebooks;
}

The Book class is a POJO:

public class Book {

    private static final long serialVersionUID = 1L;

    private Integer documentid;

    private String corpusid;

    private String corpusoriginalid;

    private float nett;

    private String currencyid;

    private int vat;

    private Discount discount;

    private Upoluj isbn;

    public Bandwcorpus() {

    }
}

My applied get function is based on http://www.adam-bien.com/roller/abien/entry/jax_rs_returning_a_list blog post and this solution was applied also in Java ee 7 tutorial.

Unfortunately, I get the following compile error in this case:

error: no suitable method found for get(<anonymous com.fasterxml.classmate.GenericType<List<Book>>>)
    List<Book> ebooks=restClient.target("http://bookandwalk.pl/api/admindocumentlist").queryParam("password", "XXXXX").queryParam("corpusid", "YYYYY").request(MediaType.APPLICATION_JSON).get(new GenericType<List<Book>>() {});
method SyncInvoker.<T#1>get(Class<T#1>) is not applicable
  (cannot infer type-variable(s) T#1
    (argument mismatch; <anonymous com.fasterxml.classmate.GenericType<List<Bandwcorpus>>> cannot be converted to Class<T#1>))
method SyncInvoker.<T#2>get(javax.ws.rs.core.GenericType<T#2>) is not applicable
  (cannot infer type-variable(s) T#2
    (argument mismatch; <anonymous com.fasterxml.classmate.GenericType<List<Book>>> cannot be converted to javax.ws.rs.core.GenericType<T#2>))

where T#1,T#2 are type-variables:

  • T#1 extends Object declared in method <T#1>get(Class<T#1>)

  • T#2 extends Object declared in method <T#2>get(javax.ws.rs.core.GenericType<T#2>)

Any idea what do I make wrong if this is the suggested way by Oracle and other experts?

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
Roland
  • 327
  • 7
  • 19
  • Could you post your structure for Bandwcorpus – Ironluca Sep 23 '16 at 12:00
  • @Ironluca, It was a typo. Everywhere you see Bandwcorpus it is the Book class I just copied it from the earlier version. – Roland Sep 24 '16 at 17:14
  • Try this and check, ArrayList obj=new ArrayList<>(); List ebooks=restClient.target("http://bookandwalk.pl/api/admindocumentlist").queryParam("password", "XXXX").queryParam("corpusid", YYYY").request(MediaType.APPLICATION_JSON).get(obj.getClass()); – Ironluca Sep 26 '16 at 07:50
  • After checking your code I got the following runtime error: javax.ws.rs.client.ResponseProcessingException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json;charset=utf-8, type=class java.util.ArrayList, genericType=class java.util.ArrayList. – Roland Sep 26 '16 at 09:32
  • Check if Jackson provider is configured corretly, refer http://stackoverflow.com/questions/23442440/messagebodyreader-not-found-for-media-type-application-json – Ironluca Sep 26 '16 at 09:54

2 Answers2

7

Finally I found the solution:

List<Book> e=restClient
    .target("http://bookandwalk.pl/api/admindocumentlist")
    .queryParam("password", "XXXXXX")
    .queryParam("corpusid_or_languagecode", "Example")
    .request(MediaType.APPLICATION_JSON)
    .get(new GenericType<List<Book>> () {});
So S
  • 581
  • 1
  • 7
  • 20
Roland
  • 327
  • 7
  • 19
  • The only annotation that could be useful, is that in the web service is not necessary to return a GeneryType> when you use @Produces(MediaType.APPLICATION_JSON) annotation, in this case, the conversion is implicit. Greetings. – martosfre Oct 10 '20 at 01:42
2

it worked like this for me, enjoy.

public List<Persona> Buscar(String estado, String namescore) {

    WebTarget resource = webTarget;
    if (estado != null) {
        resource = resource.queryParam("estado", estado);
    }
    if (namescore != null) {
        resource = resource.queryParam("namescore", namescore);
    }

    List<Persona> Personass= resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(new GenericType<List<Persona>>() {});
    return Personass;
}
ysf
  • 4,634
  • 3
  • 27
  • 29