The aim of this post is to summarize and review ways of formatting Java Time objects using Spring Boot and Jackson library.
This post is organized in five steps. Each step represents one aspect of the issue and it is also related to one commit in example project repository.
This tutorial is based on
Spring Boot version
spring-boot-starter-web. It uses
com.fasterxml.jackson.datatype in version
2.6.4, which is a default version of
Spring Boot. All of these is based on Java 8.
In the example code repository, you can find one HTTP service made with
Spring Boot. This service is a
GET operation, which returns a class with Java Time objects.
You can also find the integration test that deserializes the response.
I would like to return class
LocalDateTime, preinitialized in constructor.
Response class is serialized to JSON Map, which is a default behaviour. To some extent it is correct, but ISO formatted Strings in response are preferable.
Integration testing is an appropriate way to test our functionality.
Unfortunately, tests are not passing, because of deserialization problems. The exception with message is thrown
can not instantiate from JSON object.
First things first. We have to add JSR-310 module. It is a datatype module to make Jackson recognize Java 8 Date & Time API data types.
Note that in this example
jackson-datatype-jsr310 version is inherited from
spring-boot-dependencies dependency management.
Response is now consistent but still, not perfect. Dates are serialized as numbers:
We are one step closer to our goal. Tests are passing now because this format can deserialized without any additional deserializers.
How do I know?
Start an application server on commit
Step 2 - Adds Object Mapper, then checkout to
Step 1 - Introduce types and problems, and run integration tests without
ISO 8601 formatting is a standard. I’ve found it in many projects. We are going to enable and use it.
Edit spring boot properties file
application.properties and add the following line:
Now, the response is something that I’ve expected:
Imagine one of your client systems does not have a capability of formatting time. It may be a primitive device, or microservice that treats this date as a collection of characters. That is why special formatting is required.
We can change formatting in response class by adding
JsonFormat annotation with pattern parameter. Standard SimpleDateFormat rules apply.
Below there is a service response using custom
Our tests are still passing. It means that this pattern is used for serialization in service and deserialization in tests.
There are situations where you have to resign from
ISO 8601 formatting in your whole application, and apply custom made standards.
In this part, we will redefine format pattern for LocalDate. This will change formatting of LocalDate in every endpoint of your API.
We have to define:
DateTimeFormatterwith our pattern.
Serializerusing defined pattern.
Deserializerusing defined pattern.
ObjectMapperbean with custom serializer and deserializer.
RestTemplatethat uses our
Bean ObjectMapper is defined with annotation
@Primary, to override default configuration.
My custom pattern for
Definitions of serializer and deserializer for all LocalDate classes:
Now, the response is formatted with our custom pattern:
When we define custom serializer, our tests start to fail. It is because RestTemplate knows nothing about our deserializer. We have to create custom RestTemplateFactory that creates RestTemplate with object mapper containing our deserializer.
Custom formatting Dates is relatively simple, but you have to know how to set up it. Luckily,
Jackson works smoothly with
Spring. If you know other ways of solving this problem or you have other observations, please comment or let me know.