Spring boot定义LocalDateTime序列化

刚刚开通了一个公众号,会分享一些技术博客和自己觉得比较好的项目,同时会更新一些自己使用的工具和图书资料,后面会整理一些面试资料进行分享,觉得有兴趣的可以关注一下。
在这里插入图片描述


前言

在开发需求过程中,定义字段为LocalDateTime,返回的序列化字段是按照LocalDateTime.toString返回的,返回的字符串带一个’T’,在application.properties里面统一配置的时间格式化是不生效的。
spring.jackson.dateFormat,这个值是针对Date类型的,可以在配置类里面看。而且默认的格式其实是我们需要的
在这里插入图片描述


解决

其实有两种方法,第一种就是在字段上加一个 @JsonFormat(pattern = ""),这样也可以实现,但是系统上不可能只有这一个字段。
所以只能采用第二种,统一配置。
Jackson的配置,底层实际使用的就是ObjectMapper,在JacksonAutoConfiguration类里面配置。

		@Bean
		@Primary
		@ConditionalOnMissingBean
		ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
			return builder.createXmlMapper(false).build();
		}
		
		@Bean
		@Scope("prototype")
		@ConditionalOnMissingBean
		Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder(ApplicationContext applicationContext,
				List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
			Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
			builder.applicationContext(applicationContext);
			customize(builder, customizers);
			return builder;
		}

		private void customize(Jackson2ObjectMapperBuilder builder,
				List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
			for (Jackson2ObjectMapperBuilderCustomizer customizer : customizers) {
				customizer.customize(builder);
			}
		}

最关键的其实就是这个builder,里面有一个modules的方法可以帮我们添加新的module,对于LocalDatetime的序列化。既然builder已经配置了,我们是可以直接拿的到的。
直接上代码:

@Bean
    ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        Jackson2ObjectMapperBuilder xmlMapper = builder.createXmlMapper(false);
        builder.modules(list -> list.add(new LocalDateTimeModule()));
        return xmlMapper.build();
    }


    private class LocalDateTimeModule extends SimpleModule {

        public LocalDateTimeModule() {
            addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
    }

打完收工!

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot中,可以使用RedisTemplate来操作Redis数据库。当需要将LocalDateTime类型的数据存储到Redis中时,需要对其进行序列化和反序列化处理。 首先,需要配置RedisTemplate的序列化方式为Jackson2JsonRedisSerializer。在配置类中添加以下代码: ```java @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); // 设置key的序列化方式为StringRedisSerializer template.setKeySerializer(new StringRedisSerializer()); // 设置value的序列化方式为Jackson2JsonRedisSerializer template.setValueSerializer(new Jackson2JsonRedisSerializer<>(LocalDateTime.class)); return template; } } ``` 然后,在需要使用RedisTemplate的地方,可以直接注入RedisTemplate,并使用其提供的方法进行操作。例如,将LocalDateTime类型的数据存储到Redis中: ```java @Autowired private RedisTemplate<String, Object> redisTemplate; public void saveLocalDateTime(String key, LocalDateTime value) { redisTemplate.opsForValue().set(key, value); } ``` 同样地,可以使用RedisTemplate获取存储在Redis中的LocalDateTime类型的数据: ```java public LocalDateTime getLocalDateTime(String key) { return (LocalDateTime) redisTemplate.opsForValue().get(key); } ``` 需要注意的是,由于RedisTemplate默认使用JdkSerializationRedisSerializer进行序列化,所以需要自定义序列化方式为Jackson2JsonRedisSerializer,并指定序列化的类型为LocalDateTime
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦幻D开始

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值