1

I'm co-developing a piece of code that fetches some data via POST from an external microservice using JSON format both ways. The issue is that I'm sending an empty String, but the microservice comes back to me with an error message suggesting the fields I send are null whenever I send an empty String. Adding a space character eliminates the issue. Is there any chance that somehow the serialization is going wrong?

Thank you in advance.

This is my rest template:

final HttpEntity<?> httpRequestEntity = new HttpEntity<>(employeeRequest, copyAndAdjustHeaders(httpHeaders));
            final ResponseEntity<EmployeeResponseDto> exchange = restTemplate.exchange(employeeResourcePath, HttpMethod.POST, httpRequestEntity, EmployeeResponseDto.class);
            final EmployeeResponseDto body = exchange.getBody();

My object mapper configuration:

    @Bean
    public ObjectMapper objectMapper() {
        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        objectMapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
        objectMapper.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
        return objectMapper;
    }

And how i initialize the autowired RestTemplate in the constructor.

        this.restTemplate = restTemplateBuilder
                .setConnectTimeout(requestConnectionTimeoutMillis)
                .setReadTimeout(requestReadTimeoutMillis)
                .rootUri(employeeRootUri)
                .build();
tomaszn
  • 27
  • 1
  • 6
  • Can you please add your definition of employeeRequest? And do you have the interface of the API you are calling? – Milgo Aug 10 '20 at 10:47
  • Check out [this answer](https://stackoverflow.com/questions/44586126/issue-for-empty-string-deserialization) – Santi Barbat Aug 10 '20 at 10:57
  • @Milgo thank you for your reply. employeeRequest is a simple DTO, all fields are annotated with @JsonProperty/@NotNull (on both ends). The ones causing trouble are inside a list. ```private List notes;``` and inside ```@NotNull private String personalNotes = ""; @NotNull private String corporateNotes = "";``` – tomaszn Aug 10 '20 at 11:16
  • @tomaszn did my answer solve you problem? – Santi Barbat Sep 02 '20 at 11:19
  • @SantiBarbat Unfortunately it didn't work. The solution was to annotate the fields with ```@NotNull and @JsonInclude(JsonInclude.Include.NON_NULL).``` – tomaszn Sep 10 '20 at 10:17

2 Answers2

3

Disable the ACCEPT_EMPTY_STRING_AS_NULL_OBJECT on your mapper with:

objectMapper.disable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

ACCEPT_EMPTY_STRING_AS_NULL_OBJECT: Feature that can be enabled to allow JSON empty String value ("") to be bound to POJOs as null.

Santi Barbat
  • 2,097
  • 2
  • 21
  • 26
0

The solution that solved it was to annotate the fields with @NotNull and @JsonInclude(JsonInclude.Include.NON_NULL) in the microservice that did the posting. It was still weird though it didn't replicate every single time across different environments. Now it works fine.

tomaszn
  • 27
  • 1
  • 6
  • This would be a fix for only one field, whether if you setup `ACCEPT_EMPTY_STRING_AS_NULL_OBJECT` would be application-wide, and as far as I can tell, that was the question about. Anyways, good if it worked for you :) – Santi Barbat Sep 10 '20 at 10:35
  • It was curious from the beginning, however I did try out to configure the mapper as you recommended without sccuess. Thank you for your help! – tomaszn Sep 10 '20 at 13:07