0%

Spring Boot 通用解决 LocalDateTime 转为字符串后中间含“T”

通用解决LocalDateTime转为字符串后中间含“T”的问题 ✌️

本文参考自:https://juejin.im/post/5ceb4f156fb9a07f06554b63

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.scaffolding.base.config;

import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* 解决 LocalDateTime 中间含 T 的问题
*
* @author xuxu
*/
@Configuration
public class LocalDateTimeSerializerConfig {

@Bean
public LocalDateTimeSerializer localDateTimeDeserializer() {
return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}

@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
}
}
-------------------本文结束 感谢您的阅读-------------------
坚持原创技术分享,感谢您的支持和鼓励!