Jackson’s ObjectMapper defaults are fairly strict — unknown fields throw exceptions, dates come out as timestamps. These three configurations are the ones I almost always apply when setting up a new project. FAIL_ON_UNKNOWN_PROPERTIES false is especially important when consuming external APIs that might add new fields.
1
2
3
4
| import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
|
- Configuration to ignore unknown keys in json
1
2
| ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
- Configuration to prevent date from being written as timestamps
1
2
3
| ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
- Configuration to set missing values as null in response
1
2
3
| ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(Include.ALWAYS);
|