1

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

  • Add `spring-boot-starter-json` as a dependency and remove your custom configuration for the `ObjectMapper`. Finally `jackson.serialization` is a non-existing property and won't work. – M. Deinum Oct 25 '22 at 07:33

2 Answers2

0

If you add jackson-datatype-jsr310 on the classpath as a dependency, Spring Boot should recognize it and configure Java date types for you without having to configure anything else.

Add this to your maven pom file, along with your other dependencies:

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

As a result of this, I don't believe you need to manually configure the ObjectMapper with JavaTimeModule, and I don't think you need to tell Jackson/Spring to serialize dates as timestamps (I think this is the default now).

Todd
  • 30,472
  • 11
  • 81
  • 89
0

I solved the problem! Sorry for the delay.

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