V
V
Vlad Mistetsky2015-12-01 12:29:26
Java
Vlad Mistetsky, 2015-12-01 12:29:26

How to determine which fields have been changed when using JSON to POJO conversion?

There is a web service that accepts a JSON payload (for the PUT method).
Jackson is used to convert JSON to Java object.
There is a task to update only those fields that are present in JSON (new values ​​may be null).
The problem is that after converting the JSON to a Java object, all fields that were not present in the JSON are null and I can't figure out why they are null: because they weren't in the JSON (and I don't want to update them), or because they were there with a null value (and I want to update them).
As far as I understand, if a field was present in JSON and it had a null value there, then Jackson will call a setter for this field.
How can I find out which setters on an object have been called and which have not?
Or is there another way to solve this problem? In my opinion, this is a fairly common problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
volyihin, 2015-12-06
@volyihin

Hey! If this is needed for a small number of objects, then you can write a custom serializer in jackson, and define the policy for working with each field yourself.
Or the second option. You can write a serializer for a specific field type. For example for String. So that it sets some default value if it does not find one in json.
All this can be found on their github in the documentation.
https://github.com/FasterXML/jackson-docs/blob/mas...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question