Search code examples
javajsonjacksonpojo

Adding get-Method to POJO causes JsonMappingException


i have a POJO class, which is used for deserialization using Jackson. This class contains the default constructor and getter and setter methods for all class variables.
Because I need some advanced logic on the content of the POJO i added a method

public String getRootModuleName()

But now my unit tests fail during deserialization with the following exception:

com.fasterxml.jackson.databind.JsonMappingException: No value present (through reference chain: com.POJO["rootModuleName"])

To me it looks like Jackson recognizes a get-Method and thus derives, that there must be a similar JSON property for deserialization.
When i annotate the method with @JsonIgnore the tests succeed.

Can someone confirm that Jackson identifies required properties by get-prefix on method names?
Is @JsonIgnore a good solution or should i decouple the business logic from POJO design?


Solution

  • You are completely right, JSON Jackson recognizes property getters by "get" prefix. So, in your case the possible solutions are to use @JsonIgnore just like you did, or rename your method to have different prefix such as "retrieve", "extract" and so forth. I'd say using @JsonIgnore is the best way to go. So, in short your solution is correct