Spring Boot Preprocess Raw JSON Data

I have a RESTful Spring download API that has a registration endpoint.

Inside my class, @RestControllerI wrote a simple String value preprocessor to trim and replace strings with spaces with values null.

  @InitBinder
  public void blankStringBinder(WebDataBinder dataBinder) {
    dataBinder.setDisallowedFields("password", "confirmPassword");
    StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
    dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
  }

But when I send data from Postman as raw JSON, cropping editing does not occur. I set a breakpoint inside the method blankStringBinder, and I see that it is called for every incoming request.

It seems to WebDataBinderwork for form-data. Is there a way to make it work with raw data JSON?

+4
source share
1 answer

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();
    }
}

POJO : @JsonDeserialize(using = EmptyToNullCustomDeserializer.class). :

@JsonDeserialize(using = EmptyToNullCustomDeserializer.class)
private String content;

</" > :

, .

, 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();
    }
}

:

@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);
            }

        };
    }
}
+3

Source: https://habr.com/ru/post/1673123/


All Articles