0

I am trying to make a restful API pick up the right class when provided a JSON object.

Consider the following classes :

package org.acme.validation.model;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonSubTypes;

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="genre")
@JsonSubTypes({@JsonSubTypes.Type(value=Comic.class, name="comic")})
public class Book {

    public String genre;

    public String getTitle() {
        return "lolwut";
    }
}
package org.acme.validation.model;

import com.fasterxml.jackson.annotation.JsonTypeName;

@JsonTypeName("comic")
public class Comic extends Book {

    public String title;

    public String getTitle() {
        return this.title;
    }
}

After many reads (this is my first Java project) I tried to deserialize using Jackson annotations with abstract class inheritance, with interface implementation, and finally with generic classes, but nothing works. I got as far as coming up with the following code, which I would expect to return foobar when provided with { "genre": "comic", "title": "foobar" } as input :

package org.acme.validation.resource;

import org.acme.validation.model.Book;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;

@Path("/books")
public class BookResource {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public <T extends Book> String foobar(T book) {
            return book.getTitle();
    }
}

However all I get is

javax.ws.rs.ProcessingException: RESTEASY008200: JSON Binding deserialization error: javax.json.bind.JsonbException: Error resolving runtime type for type: T

And when I try the abstract class inheritance I get

avax.ws.rs.ProcessingException: RESTEASY008200: JSON Binding deserialization error: javax.json.bind.JsonbException: Can't create instance

What am I missing ?

I am running the application through Quarkus, FWIW.

Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76
  • Does this answer your question? [Jackson - Deserialize using generic class](https://stackoverflow.com/questions/11664894/jackson-deserialize-using-generic-class) – KYL3R Jul 06 '20 at 11:45

1 Answers1

0

I got these errors because Jackson was conflicting with JSON-B : I was trying to use Jackson annotations but the pom.xml was including JSON-B as a dependency. I just had to replace quarkus-resteasy-jsonb by quarkus-resteasy-jackson in the pom.xml and everything (generic classes, abstract class inheritance, interface implementations) worked as expected.

Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76