首页 > 编程语言 >rabbitmq发送消息localdatetime报错:Java 8 date/time type `java.time.LocalDateTime` not supported by default

rabbitmq发送消息localdatetime报错:Java 8 date/time type `java.time.LocalDateTime` not supported by default

时间:2024-07-21 23:29:47浏览次数:13  
标签:om supported 报错 time LocalDateTime new 序列化 ObjectMapper

两种解决方案:

  1. 通过全局配置LocalDateTime的序列化

    /**
     * json序列化增强 解决Jackson序列化不了Java8日期
       */
        @Bean
        public MessageConverter messageConverter() {
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        om.registerModule(new JavaTimeModule());
        return new Jackson2JsonMessageConverter(om);
        }
  2. 通过引入依赖

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

    在消息发送的代码中转换LocalDateTime类型

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JavaTimeModule());
    String s = objectMapper.writeValueAsString(direct);//实体类
    ​
    try {
         rabbitTemplate.convertAndSend("direct","queue.routingKey",s,correlationData);
      } catch (AmqpException e) {
    ​
    }

    这样可以解决问题不过每次发消息都需要加上如上代码很不方便。

    ————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    原文链接:https://blog.csdn.net/weixin_49803290/article/details/127083499

  3.  

标签:om,supported,报错,time,LocalDateTime,new,序列化,ObjectMapper
From: https://www.cnblogs.com/dragon-proud-sky/p/18315128

相关文章