LocalDateTime converter to JSON with Kotlin

When I tried to convert special data types like LocalDateTime into JSON I ran against a wall. It took me hours to find the solution. If you face the same problem, here is the solution:


The first thing you need are the libraries that contain the neccessary converters:

<!-- Jackson -->
<dependency> <!-- Java 8 Parameter names -->
<dependency> <!-- Java 8 Optional -->
<dependency> <!-- Java 8 LocalDate/Time -->
<dependency> <!-- Kotlin class and data classes without default constructor -->

For our problem with LocalDateTime only jackson-datatype-jsr310 is important. But with jackson-module-parameter-names you get the Java 8 feature of accessing parameter names without annotating them. jackson-datatype-jdk8 offers you the possibility to use e.g. Optional for a JSON interface. And last but not least jackson-module-kotlin add the power of Kotlin constructors to JSON converters.


As often, I use Spring as a base for my development. So I need a configuration that tells Spring to add the afore mentioned modules to the ObjectMapper of Jackson. This is done by the following code snippet:

fun objectMapper(): ObjectMapper =

As you can see, this is Kotlin code that perfectly integrates into your Spring application.

Usage in the DTO

My DTO class now can use any format definition for the JSON output of the date:

@get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy hh:mm:ss")
val changeTime: LocalDateTime? = null,

This a common german style of displaying a local date and time. It could look like „09.05.2017 19:00:00“.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.