Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing a derived class using Jackson java
    primarykey
    data
    text
    <p>I have a base class as follows:</p> <pre><code>public class Criteria { private CriteriaType type; //getters &amp; setters public enum CriteriaType { Condition, Medication } } </code></pre> <p>and derived classes</p> <pre><code>public class ConditionCriteria extends Criteria { private String a; //getters &amp; setters } public class MedicationCriteria extends Criteria { private String b; //getters &amp; setters } </code></pre> <p>and another class</p> <pre><code>public class CriteriaGroup { Criteria criteria; //getters &amp; setters } </code></pre> <p>I send a serialized JSON string of <code>CriteriaGroup</code> class to the server using Jackson for (de)serialialization. And the service on the server looks like:</p> <pre><code>@Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response createCriteriaGroup(CriteriaGroup criteriaGroup) {... </code></pre> <p>However, JAckson does not convert the deserialized JSON to one of the derived classes. How can I achieve this mapping according to the <code>type</code> field of <code>Criteria</code> class?</p> <p>Thanks..</p> <p>I get an exception like this when deserializing a JSON of ConditionCriteria:</p> <pre><code>SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "a" (Class Criteria), not marked as ignorable at [Source: org.eclipse.jetty.server.HttpInput@3c92218c; line: 1, column: 92] (through reference chain: CriteriaGroup["criteria"]-&gt;Criteria["a"]) at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53) at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267) </code></pre> <p>....</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload