Question

I am currently using Jackson 1.4.2 and attempting deserialization of code values (unique identifiers for type information) that are passed from our UI back to the Java controllers (Servlets).

There are multiple types (e.g. ABCType, XYZType, etc.) that all extend from an AbstractType, but each concrete type has a static factory method that takes as a single parameter, a unique identifier, and returns the type object (name, associated types, description, valid acronyms, etc.) represented by that identifier. The static method within each concrete type (e.g. XYZType) is annotated with @JsonCreator:

@JsonCreator
public static XYZType getInstance(String code) {
    .....
}

The problem that I am seeing though is an exception thrown by Jackson's mapper trying to deserialize the json to those types:

Caused by: org.codehaus.jackson.map.JsonMappingException: No default constructor found for type [simple type, class com.company.type.XYZtype]: can not instantiate from Json object.

What am I missing here of the @JsonCreator annotation to static factory methods (or is it to do with Jackson 1.4.2 struggling with the concrete types extending from an AbstractType?)?

Was it helpful?

Solution

The annotation @JsonCreator requires the annotation @JsonProperty. This Jackson wiki page gives little information but does offer sample code:

@JsonCreator
public Name(@JsonProperty("givenName") String g, @JsonProperty("familyName") String f)
{
  givenName = g;
  familyName = f;
}

You'll find a more detailed explanation at this blog post.

Your sample code should therefore look something like this:

@JsonCreator
public static XYZType getInstance(@JsonProperty("someCode") String code)
{
 ...
}

OTHER TIPS

Problem is that Jackson only sees the declared base type, and does not know where to look for subtypes. Since full polymorphic type handling was added in 1.5, what you need to do with 1.4 is to add factory method in the base class and dispatch methods from there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top