jackson annotations being ignored
Solution 1
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.
Solution 2
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());
Related videos on Youtube
user868512
Updated on September 15, 2022Comments
-
user868512 over 1 year
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; }
-
user868512 over 10 yearsJust to let others know, this was the problem I was having. Switched to importing 1.x annotation package and all is now well.
-
CorayThan about 10 years@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.