Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading