1

I'm using kotlin with spring boot. When i request to endpoint, that should return me LocalDateTime object, it returns me array instead of string.

Example:

"createdAt": [
            2020,
            12,
            16,
            23,
            26,
            10,
            932070000
        ]

What i expect:

"createdAt": "2020-12-16 23:26:10"

I created Jackson2ObjectMapperBuilderCustomizer but looks like it's not working at all

@Configuration
class JacksonObjectMapperConfiguration {

companion object {
    private const val dateFormat = "yyyy-MM-dd"
    private const val dateTimeFormat = "yyyy-MM-dd HH:mm:ss"
}

@Bean fun jackson2ObjectMapperBuilderCustomizer(): Jackson2ObjectMapperBuilderCustomizer {
    return Jackson2ObjectMapperBuilderCustomizer {
        builder -> builder.simpleDateFormat(dateFormat)
                   builder.serializers(LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)))
                   builder.serializers(LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)))
    }
}

@Bean
@Primary
fun objectMapper(): ObjectMapper =
        ObjectMapper()
                .registerModule(KotlinModule())

}

My build.gradle.kts with jackson dependency

implementation(group="com.fasterxml.jackson.module", name="jackson-module-kotlin", version="2.11.3")

I want to create global LocalDateTime and LocalDate serializer for all fields in my app, any ideas how to handle it?

Yar Sol
  • 197
  • 2
  • 15
  • You would need to register the `JavaTimeModule` too for objectMapper. Try out this [answer](https://stackoverflow.com/questions/32952269/cant-serialize-java-time-localdate-as-a-string-with-jackson/58387888#58387888) of mine, it's in Java though but similar. – Madhu Bhat Dec 20 '20 at 15:10
  • 1
    @MadhuBhat sorry but this didn't help – Yar Sol Dec 20 '20 at 18:21

1 Answers1

2

Issue fixed after i removed @EnableWebMvc on one of my configuration classes.

Yar Sol
  • 197
  • 2
  • 15
  • For me it was to remove `@EnableWebFlux` on a `@Configuration` class where I was implementing `WebFluxConfigurer` and providing CORS mappings. Thanks for the tip. Like you, I tried nearly everything for custom json serialization to no avail. – haventchecked Mar 18 '21 at 03:30