I'm puzzled as to why using a default inclusion of jackson that Spring seems to have customised the default Jackson configuration.
One setting it's messing with is WRITE_DATES_AS_TIMESTAMPS
, the Jackson default is true
however Spring has somewhere changed this to false
and also provided a date format.
Where in the world is this happening? I want my dates to remain serialised as numbers.
UPDATE: Turns out it's not spring that's causing the problem, it's actually hibernates proxy classes causing the problem. For some reason if hibernate has a type-mapping of type="date"
it serialises as a date string, though if its type="timestamp"
it serialises as expected. Rather than spend too much time looking into this I've decided to just change all my mappings to timestamp for now.
Auto-configuration for Jackson is provided and Jackson is part of spring-boot-starter-json . When Jackson is on the classpath an ObjectMapper bean is automatically configured. Several configuration properties are provided for customizing the configuration of the ObjectMapper .
Overview. When using JSON format, Spring Boot will use an ObjectMapper instance to serialize responses and deserialize requests. In this tutorial, we'll take a look at the most common ways to configure the serialization and deserialization options. To learn more about Jackson, be sure to check out our Jackson tutorial.
Simplest usage is of form: final ObjectMapper mapper = new ObjectMapper(); // can use static singleton, inject: just make sure to reuse! MyValue value = new MyValue(); // ... and configure File newState = new File("my-stuff. json"); mapper.
ObjectMapper is the main actor class of Jackson library. ObjectMapper class ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions.
Starting with 3.1 M1 you can specify jackson custom configuration by registering an HttpMessageConverters
through a sub-element of mvc:annotation-driven
.
See Spring 3.1 MVC Namespace Improvements
See SPR-7504 Make it easier to add new Message Converters to AnnotationMethodHandlerAdapter
Exemple:
<bean id="jacksonObjectMapper" class="x.y.z.CustomObjectMapper">
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
The CustomObjectMapper Object
@Component("jacksonObjectMapper")
public class CustomObjectMapper extends ObjectMapper {
@PostConstruct
public void afterPropertiesSet() throws Exception {
SerializationConfig serialConfig = getSerializationConfig()
.withDateFormat(null);
//any other configuration
this.setSerializationConfig(serialConfig);
}
}
SerializationConfig .withDateFormat
In addition to constructing instance with specified date format, will enable or disable Feature.WRITE_DATES_AS_TIMESTAMPS (enable if format set as null; disable if non-null)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With