Q:

Spring Boot Preprocess JSON data

I have a RESTful Spring Boot API, that has a registration end point. 
Inside my @RestController class I have written a simple String value pre-processor, to trim and replace whitespace only strings with null values.
  @InitBinder
  public void blankStringBinder(WebDataBinder dataBinder) {
    dataBinder.setDisallowedFields("password", "confirmPassword");
    StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
    dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
  }

But when I submit the data from Postman as raw JSON, the trimming edit is not taking place. I put a break point inside blankStringBinder method and I see that it gets called on each incoming request. 
The WebDataBinder seems to work for form-data. Is there a way to make it work for raw JSON data too?

A:

Create a custom JsonDeserializer:
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

public class EmptyToNullCustomDeserializer extends JsonDeserializer<String> {
    @Override
    public String deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
        JsonNode node = jp.readValueAsTree();
        if (node.asText().isEmpty()) {
            return null;
        }
        return node.toString();
    }
}

And on each String of the POJO, add this annotation : @JsonDeserialize(using = EmptyToNullCustomDeserializer.class).
For example : 
@JsonDeserialize(using = EmptyToNullCustomDeserializer.class)
private String content;

Edit :
If you have got a lot of Strings, it can be cumbersome.
In order to generalize to all the strings, you have to modify EmptyToNullCustomDeserializer :
@Component
public class EmptyToNullCustomDeserializer extends StdDeserializer<String> {
    protected EmptyToNullCustomDeserializer() {
        super(String.class);
    }

    @Override
    public String deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
        JsonNode node = jp.readValueAsTree();
        if (node.asText().isEmpty()) {
            return null;
        }
        return node.toString();
    }
}

And create this component :
@Component
public class JacksonConfiguration {

    @Autowired
    private EmptyToNullCustomDeserializer emptyToNullCustomDeserializer;

    @Primary
    @Bean
    public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder(List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        for (Jackson2ObjectMapperBuilderCustomizer customizer : customizers) {
            customizer.customize(builder);
        }
        return builder;
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer addEmptyToNullStringDeserialization() {
        return new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
                jacksonObjectMapperBuilder.deserializerByType(String.class, emptyToNullCustomDeserializer);
            }

        };
    }
}

