在Spring Boot中使用fastjson来处理JSON格式转换时,默认情况下是不支持JDK1.8中的LocalDateTime及LocalDate等时间对象的。为使其支持这些对象,可以定义相关的ObjectDeserializer来处理,并将定义的对象通过ParserConfig.getGlobalInstance().putDeserializer方法将其作为全局配置。
实现如下:
@Configuration
public class JSONConfiguration {
@PostConstruct
public void init() {
ParserConfig.getGlobalInstance().putDeserializer(LocalDate.class, new LocalDateDeserializer());
ParserConfig.getGlobalInstance().putDeserializer(LocalDateTime.class, new LocalDateDeserializer());
}
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(

本文介绍了在Spring Boot应用中,如何使fastjson支持JDK8的LocalDate和LocalDateTime。默认fastjson不支持这些类型,解决办法是自定义ObjectDeserializer并全局配置。在实现过程中,调用parser.getLexer().nextToken()是关键,以避免转换错误。

3万+

被折叠的 条评论
为什么被折叠?



