6

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;
    }
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
user868512
  • 67
  • 1
  • 1
  • 3
  • Possibly a duplicate of http://stackoverflow.com/questions/15436268/unable-to-upgrade-to-jackson-2-1-4-jersey-ignoring-the-annotations which has the better answer: why downgrade your annotations when you can upgrade Jersey to use Jackson 2 – FelixJongleur42 Aug 07 '14 at 15:13

2 Answers2

30

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.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • 3
    Just to let others know, this was the problem I was having. Switched to importing 1.x annotation package and all is now well. – user868512 Sep 16 '13 at 16:43
  • 2
    @StaxMan Haha, yeah, just in case it gets lost in this forest of answers! But honestly, it's too bad he didn't accept it. – CorayThan Mar 11 '14 at 16:49
3

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());
PAX
  • 1,056
  • 15
  • 33