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.