Skip to content
Advertisement

@JsonProperty not working after springboot upgrade

I recently upgraded my application from Spring Boot 1.5.3 to Spring Boot 2.3.5. One of the feature on UI started breaking and I found while debugging that the json response to UI had changed

Original response:

JavaScript

New response: having underscores (_) in attribute names

JavaScript

It seems that the @JsonProperty is not working. My classes are as below:

JavaScript

I tried @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) but it doesn’t work. Found another solution on stack-overflow where it was suggested to replace the jackson annotation import to org.codehaus.jackson.annotate but this didn’t work for me either.

The same classes are being used for 2 purpose here, 1. @JsonProperty is being used to map json response from anotherService to java object and then the same java object is being used to serialise the jave object to ui. I cannot understand how was it working before the ugrade.

Any help is greatly apreciaed as this has already taken one day but I couldn’t identify and resolve the issue yet.

Advertisement

Answer

I cannot replicate this using Jackson 2.11.3 which is pulled in by Spring Boot 2.3.5

The following test correctly serializes with camelCase.

JavaScript

Output:

JavaScript

Now, that was not an answer so I’ll provide one here, though it’s a workaround since the issue seems to be somewhere else than in the code you’ve posted.

You haven’t told us which version of Java you’re using so I’m assuming it’s 8 or later, if you’re on 7 or earlier this won’t work.

You can drop all the @JsonProperty annotations, add the Jackson Java 8 Parameter Names module (to make up for the removed annotations) and employ two ObjectMappers with different naming strategies:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement