views:

203

answers:

1

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?)?

+1  A: 

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.

StaxMan