-1

I'm getting something like this in my JSON response (I'm having a REST implementation in SpringBoot):

"estimatedDeliveryTimeWindow":{  
  "window":{}
}

I have set custom HTTPMessageCOnverters and configured objectMapper like this:

objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

Also tried to remove default converters using below code:

@Bean
public HttpMessageConverters converters() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        jsonConverter.setObjectMapper(objectMapper);
        return new HttpMessageConverters(false, Arrays.asList(jsonConverter));
}

Nothing seems to work. I still see null objects within objects. These objects are complex objects nested with primitive types and custom objects. What else I can try?

Shrey Garg
  • 1,317
  • 1
  • 7
  • 17
  • Might be a duplicate: https://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null – Michal Rorat Jun 28 '19 at 06:36
  • 1
    Possible duplicate of [How to tell Jackson to ignore a field during serialization if its value is null?](https://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null) – cassiomolin Jun 28 '19 at 10:05
  • The JSON structure you're showing does not have any `null` values. Is that the result you currently have, or the desired result? – g00glen00b Jun 28 '19 at 13:14

2 Answers2

1

Please add @JsonInclude(Include.NON_NULL) before the class files

@JsonInclude(Include.NON_NULL)
public class MobileLoginVO {

    private String otpDetailsId;

    public String getOtpDetailsId() {
        return otpDetailsId;
    }

    public void setOtpDetailsId(String otpDetailsId) {
        this.otpDetailsId = otpDetailsId;
    }

}
RAJKUMAR NAGARETHINAM
  • 1,408
  • 1
  • 15
  • 26
0

You need to inform somehow to spring to use your message converter.

This should do the work:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    public MappingJackson2HttpMessageConverter messageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        jsonConverter.setObjectMapper(objectMapper);

        return jsonConverter;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(messageConverter());
    }
}
Daniel Taub
  • 5,133
  • 7
  • 42
  • 72