无法编写 JSON:Java 8 日期/时间类型 `java.time.LocalDate`

问题描述 投票:0回答:3

我正在使用 Spring Boot 2.7.4

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

尝试通过

messagingTemplate.convertAndSend
QueueMessagingTemplate

发送对象时,我遇到以下错误

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 默认情况下不支持 Java 8 日期/时间类型

java.time.LocalDate
:添加模块“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
)

我已经尝试过添加

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()
}

jackson:
    serialization:
      WRITE_DATES_AS_TIMESTAMPS: false

我没有将

@EnableWebMvc
置于申请类之上

spring-boot kotlin amazon-sqs jackson-databind
3个回答
2
投票

我解决了问题!抱歉耽搁了。

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

0
投票

如果您在类路径上添加 jackson-datatype-jsr310 作为依赖项,Spring Boot 应该能够识别它并为您配置 Java 日期类型,而无需配置任何其他内容。

将其与其他依赖项一起添加到您的 Maven pom 文件中:

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

因此,我不认为您需要使用

ObjectMapper
手动配置
JavaTimeModule
,而且我认为您不需要告诉 Jackson/Spring 将日期序列化为时间戳(我 think 这现在是默认值)。


0
投票

我像这样解决了它,请注意它不需要是静态的:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

public class StringUtility {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    static {
        MAPPER.registerModule(new JavaTimeModule());
        MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    }

    private StringUtility() {
    }

    public static String asJsonString(Object object) {
        try {
            return MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}

您将需要此依赖项:

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

但是,如果你使用这个:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

这包含所有必需的传递依赖项

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.