Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson not recognizing @JsonCreator annotation

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

like image 647
user376327 Avatar asked Jun 25 '10 14:06

user376327


People also ask

What is @JsonCreator in Java?

@JsonCreator is used to fine tune the constructor or factory method used in deserialization. We'll be using @JsonProperty as well to achieve the same. In the example below, we are matching an json with different format to our class by defining the required property names.

How do I ignore JsonProperty?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

What is JsonProperty annotation in spring boot?

The @JsonProperty annotation is used to map property names with JSON keys during serialization and deserialization. By default, if you try to serialize a POJO, the generated JSON will have keys mapped to the fields of the POJO.

What is the use of @JsonIgnore annotation?

@JsonIgnore is used to ignore the logical property used in serialization and deserialization. @JsonIgnore can be used at setters, getters or fields. If you add @JsonIgnore to a field or its getter method, the field is not going to be serialized.


2 Answers

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)
{
 ...
}
like image 181
Paul Avatar answered Nov 08 '22 13:11

Paul


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.

like image 28
StaxMan Avatar answered Nov 08 '22 13:11

StaxMan