0

In Quarkus, I expose a PUT method that allows to modify an Animal, which can be a Dog or a Cat

Here is the method signature I want to have:

@PUT
@Path("{id}")
public Response updateAnimal(@PathParam Long id, Animal animal) {}

I want the frontend to send a Dog or a Cat, that would be deserialized as an Animal inside Quarkus. But when the frontend sends a Dog or a Cat, i get this error:

SEVERE [org.ecl.yas.int.Unmarshaller] (executor-thread-199) Can't create instance

The frontend sends a json object with all the attributes of a Dog or a Cat, and I am pretty sure Quarkus doesn't manage to transform the json into a supertype object.

public abstract class Animal {
   private String name;
   ...
}

public class Dog extends Animal {
   private String dogAttribute;
   ...
}

public class Cat extends Animal {
   private String catAttriute;
}

Example of json sent by the frontend in the body:

{
   "name": "toto",
   "dogAttribute": "dog"
}
  • I would certainly introduce a `type` property in `Animal` (and the payload send from the client) so you can configure Jackson (or any other (de)serialization tool used) to map the payload to the specific subclass. – Glains Aug 31 '20 at 12:03
  • Thanks for your reply, but I don't get it I ended up doing 2 different endpoints : one for each type – Guy Desirer Sep 01 '20 at 20:52
  • You would need to configure Jackson to use deduction mode for deserialization. See here for example https://github.com/FasterXML/jackson-databind/pull/2813 – Hitobat Sep 04 '20 at 21:17
  • Thanks for the reply, yes I was also thinking about that. I tried to configure Jackson, but I didn't manage. Since it was fine to keep 2 endpoints instead of one for both "Animal", I stopped to look for the right Jackson configuration – Guy Desirer Sep 05 '20 at 22:42

0 Answers0