The incoming JSON is mapped to a POJO. There are fields in this POJO that can be absent in the incoming JSON in some scenarios. These fields can be sent as null values too.Is there a way to differentiate between when the fields are sent as null values and when the fields are not sent at all in the JSON. The default value of the fields is null. The processing is different when the field is sent as null from when it is absent in the request JSON. I'm on spring boot and java 8.
Asked
Active
Viewed 1,844 times
6
-
Possible duplicate of [java/jackson Differentiate between null and unset field](https://stackoverflow.com/questions/45397204/java-jackson-differentiate-between-null-and-unset-field) – pvpkiran Apr 12 '18 at 13:09
-
See also: http://www.baeldung.com/jackson-optional Covers usage of `Optional` for exactly this purpose. – lexicore Apr 12 '18 at 13:13
-
@lexicore Using `Optional` as a field and a method parameter as in that example is rather odd and in most cases an anti-pattern. – Mark Rotteveel Apr 12 '18 at 14:38
1 Answers
0
I do not know of any way to automaticallly configure Jackson (or any other parser that I know of) to set some default values in case of null-valued property.
What you can do is have a flag in the setter methods that can be set if the method is called. setter methods are called only for properties that were found in the input.
EDIT:
apparently, there is a way to tell jackson what to do in case of null value properties: JsonDeserializer
(called from ObjectMapper
) has a method getNullValue()
that can be overriden to specify special value or otherwise handling this case. there is even a getEmptyValue()
to handle empty String input values!

Sharon Ben Asher
- 13,849
- 5
- 33
- 47