Question

I'm tryin to consume this REST service

@POST
@Path(value="/storeGeneric")
@Consumes(MediaType.APPLICATION_JSON)
public <T> void storeGeneric(T data) {
    try {
        System.out.println("data name: "+data.getClass().getCanonicalName());
        ...
    } catch (Exception e) {
        e.printStackTrace();
    }
}

by passing a JSON object by this javascript function:

function sendArticolo() {
    var articolo = {};
    articolo.id = 1;
    articolo.prezzo = 1;
    articolo.descrizione="roba";
    try {
        $.ajax({
            url: 'http://localhost:8080/ZZCrudRest/services/Rest/storeArticolo',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(articolo),
            dataType: 'json'
        });
    } catch (err) {
        alert(err.message);
    }
}

but, I got this exception:

12:37:26,294 GRAVE [com.sun.jersey.spi.container.ContainerResponse] (http-127.0.0.1-127.0.0.1-8080-1) The RuntimeException could not be mapped to a response, re-thr
owing to the HTTP container: java.lang.ClassCastException: java.lang.reflect.Method cannot be cast to java.lang.Class
        at com.owlike.genson.reflect.TypeUtil.getTypes(TypeUtil.java:362) [genson-0.94.jar:]
        at com.owlike.genson.reflect.TypeUtil.match(TypeUtil.java:298) [genson-0.94.jar:]
        at com.owlike.genson.convert.BasicConvertersFactory.provide(BasicConvertersFactory.java:102) [genson-0.94.jar:]
        at com.owlike.genson.convert.BasicConvertersFactory.create(BasicConvertersFactory.java:74) [genson-0.94.jar:]
        at com.owlike.genson.convert.BasicConvertersFactory.create(BasicConvertersFactory.java:56) [genson-0.94.jar:]
(more and more...)

Is there a way to consume a REST service with a generic parameter using JSON? I googled a lot without success. I'm using Jersey 1.8, genson 0.94, jboss 7.1.

Était-ce utile?

La solution

This won't work Genson does not know the type of T so it can not correctly deserialize it. BTW there is no way to know the type here (with or without genson). A solution with genson would be to:

1) change the signature to : public void storeGeneric(Object data)

2) then add the type of your root object in the json on client side (js code), ex suppose you wan't to deserialize to com.mypackage.FooBar then in your json you must have: {"@class": "com.mypackage.FooBar", ... the rest of the properties ...}

note: "@class": "yourclass" must be the first key/value pair of the object.

Genson will get the type from there.

On server side you must enable support of polymorphic types in Genson by doing:

@Provider
public class GensonCustomResolver implements ContextResolver<Genson> {
  // configure the Genson instance
  private final Genson genson = new Genson.Builder().setWithClassMetadata(true).create();

  @Override
  public Genson getContext(Class<?> type) {
      return genson;
  }
}

Also 0.94 is quite old, you should upgrade to latest release: 0.98

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top