Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jackson annotations being ignored

I'm trying to use Jackson annotations to re-name some of the json labels produced during serialization. The annotations all compile fine, and when I run, the Jackson serialization works except all Jackson annotations are completely ignored. Even the basic ones like @JsonIgnore or @JsonProperty have no effect on the json response. The libraries I have in my build path are:

jsr311-qpi-1.1.1.jar
jackson-[core|databind|annotations]-2.2.0.jar

I'm running within Eclipse running the jetty external program with the External program setup as:

Location: .../apache-maven-2.2.1/bin/mvnDebug.bat
working Directory: ${workspace_loc:/ohma-rest-svr}
Arguments: jetty:run

with the Remote Java Application configuration set as:

Host: localhost
Port: 8000

With no error messages to work from, I'm at a bit of a loss of things to try. Any ideas would be appreciated.

Here's a bit of code sample from a class I need to serialize:

@XmlRootElement(name="ads-parameter")
public class DefineParameterResponse {

    private Date _createdAt = new Date();

    @JsonProperty("created-at")
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd,HH:00", timezone="CET")
    @XmlElement
    public String getCreatedAt() {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(_createdAt);
    }

    @JsonProperty("created-at")
    public void setCreatedAt(Date createdAt) {
        this._createdAt = createdAt;
    }


    private String _dataTitle1 = "Default Title1";
    @XmlElement
    @JsonProperty("data-title-1")
    public String getDataTitle1() {
        return _dataTitle1;
    }

    @JsonProperty("data-title-1")
    public void setDataTitle1(String dataTitle1) {
        this._dataTitle1 = dataTitle1;
    }


    @XmlElement
    @JsonProperty("data-title-2")
    public String getDataTitle2() {
        return _dataTitle2;
    }

    @JsonProperty("data-title-2")
    public void setDataTitle2(String dataTitle2) {
        this._dataTitle2 = dataTitle2;
    }
like image 275
user868512 Avatar asked Sep 09 '13 17:09

user868512


People also ask

How do you tell Jackson to ignore a field during serialization?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.

What is the use of Jackson annotations?

This annotation is used to specify a custom deserializer in order to unmarshall a JSON object. Using this annotation, we use a default value for deserializing an unknown enum value. Using this annotation, you can mark a property or a group of properties to be ignored. This is done at the class level.

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.

How do I ignore properties in Jackson?

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON.


2 Answers

One relatively common reason is trying to use "wrong" set of annotations: Jackson 1.x and Jackson 2.x annotations live in different Java packages, and databind has to match major version. This design has the benefit of allowing 1.x and 2.x versions to be used side by side, without class loading conflicts; but downside that you have to make sure that you have matching versions.

Biggest problem is the use by frameworks: many JAX-RS implementations (like Jersey) still use Jackson 1.x by default. So I am guessing you might be using Jackson 1.x indirectly, but adding Jackson 2.x annotations. If so, you need to use 1.x annotations (ones under org.codehaus.jackson) instead.

like image 82
StaxMan Avatar answered Sep 28 '22 21:09

StaxMan


Just in case that somebody hits a similiar problem where only @JsonFormat gets ignored:

Consider that in Spring Boot + Java 8 context the processing of LocalDateTime may experience troubles. Instead of following dependency:

 compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jdk8'

You should use:

 compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310'

Furthermore, when you create your Jackson ObjectMapper you need to register the implementation which treats LocalDateTime correctly:

 json = new ObjectMapper().findAndRegisterModules().writeValueAsString(entity);
 new ObjectMapper().findAndRegisterModules().readValue(json, entity.getClass());
like image 21
PAX Avatar answered Sep 28 '22 23:09

PAX