spring-bootkotlinamazon-sqsjackson-databind

Could not write JSON: Java 8 date/time type `java.time.LocalDate`


I am using Spring Boot 2.7.4

and

<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-kotlin</artifactId>
</dependency>

I'm facing the following error when trying to send an Object through messagingTemplate.convertAndSend from QueueMessagingTemplate

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type java.time.LocalDate not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310"

@Service
class SqsMessageSender(
    private val messagingTemplate: QueueMessagingTemplate,
) {

    fun send(operation: DesafioExactaQueueOperation, @Valid dto: BillDTO){

        val message = SqsPayload(operation, dto)

        MessageBuilder
            .withPayload(message)
            .build()

        messagingTemplate.convertAndSend(Queues.DESAFIO_EXACTA, message)
    }
}

class SqsPayload(
    val operation: DesafioExactaQueueOperation,
    val body: Any //Must to be Any
)

class BillDTO(

    @JsonProperty("code")
    val code: UUID,

    @JsonProperty("value")
    val value: BigDecimal,

    @JsonProperty("expireAt")
    @JsonFormat(pattern = "yyyy-MM-dd", shape = JsonFormat.Shape.STRING)
    val expireAt: LocalDate
)

I have already tryed add

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary

@Configuration
class JacksonModuleConfig {

    @Bean
    @Primary
    fun objectMapper(): ObjectMapper =
        JsonMapper.builder()
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .addModule(JavaTimeModule())
            .build()
}

and

jackson:
    serialization:
      WRITE_DATES_AS_TIMESTAMPS: false

I did not place @EnableWebMvc over application class


Solution

  • I solved the problem! Sorry for the delay.

        @JsonProperty("expireAt")
        @JsonFormat(pattern = "yyyy-MM-dd")
        @JsonSerialize(using = LocalDateSerializer::class)
        val expireAt: LocalDate