啥也不说先贴两个代码:
一:MVC层配置
@Configuration public class JacksonConfig { /** 默认日期时间格式 */ public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; /** 默认日期格式 */ public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd"; /** 默认时间格式 */ public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss"; @Bean public ObjectMapper objectMapper(){ ObjectMapper objectMapper = new ObjectMapper(); return initObjectMapper(objectMapper); } public static ObjectMapper initObjectMapper(ObjectMapper om) { om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); om.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))); javaTimeModule.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))); javaTimeModule.addSerializer(LocalTime.class,new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT))); javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))); javaTimeModule.addDeserializer(LocalDate.class,new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))); javaTimeModule.addDeserializer(LocalTime.class,new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT))); om.registerModule(javaTimeModule).registerModule(new ParameterNamesModule()); return om; } }
二、关于数据层MyBatis需要配置pom新增依赖:
<!--mybatis对LocalDateTime的支持--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-typehandlers-jsr310</artifactId> <version>1.0.1</version> </dependency> <!--json对LocalDateTime的支持--> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.9.9</version> </dependency>
原文地址:https://www.cnblogs.com/lcmlyj/p/11792912.html
时间: 2024-10-10 09:30:07