3

I've got a POJO with several String properties

public class Pojo {
 private String firstName;
 private String lastName;
 ....
//getters...
//settesr...

Whats the best way to tell Jackson to destabilize the properties so that empty strings will map to NULL values.
I do no wish to do that in the getters (as I have a lot of POJO properties), is there a simple annotation for that (on the class level)?

opeled
  • 153
  • 1
  • 4
  • 12
  • ^ Use the answer above to define a deserializer, then annotate your field with `@JsonDeserialize`, using that deserializer. – Savior Apr 27 '16 at 15:31
  • Thanks, however i do not wish to add an `@JsonDeserializ` for each field (this is too verbose). I'm looking for a class annotation to do that... – opeled Apr 27 '16 at 15:42
  • There isn't one. You can register the deserializer for all `String` fields. – Savior Apr 27 '16 at 15:49

1 Answers1

3

Configure your mapper to enable the ACCEPT_EMPTY_STRING_AS_NULL_OBJECT feature.

Determines whether empty String value is accepted as null value for regular POJOs ("beans") with data-binding: this can be useful when dealing endpoints written in a language that has loose typing and may represent missing objects as Empty Strings.

GuiSim
  • 7,361
  • 6
  • 40
  • 50
  • 1
    Thanks for the answer, however String is not considered as a regular POJO. Thus, in my example, the lastName property is still set to an empty string ("") in case the json that is serialized is: {"firstNsme": "'john", "lastName": ""} – opeled Apr 27 '16 at 15:21
  • The linked post in the comments already explains why this doesn't work for `String`s. – Savior Apr 27 '16 at 16:21