| | |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.context.annotation.EnableAspectJAutoProxy; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.format.DateTimeFormatterBuilder; |
| | | import java.util.TimeZone; |
| | | |
| | | /** |
| | |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | // 全局忽略未知字段 |
| | | mapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| | | // Date 字段统一按 yyyy-MM-dd 输出,不再返回时分秒 |
| | | mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); |
| | | JavaTimeModule module = new JavaTimeModule(); |
| | | // LocalDateTime:支持 yyyy-MM-dd HH:mm:ss |
| | | // LocalDateTime:返回统一为 yyyy-MM-dd,入参兼容 yyyy-MM-dd 与旧的 yyyy-MM-dd HH:mm:ss |
| | | DateTimeFormatter dateTimeFormatter = |
| | | DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | DateTimeFormatter dateTimeDeserializerFormatter = |
| | | new DateTimeFormatterBuilder() |
| | | .appendPattern("yyyy-MM-dd") |
| | | .optionalStart().appendPattern(" HH:mm:ss").optionalEnd() |
| | | .toFormatter(); |
| | | module.addDeserializer(LocalDateTime.class, |
| | | new LocalDateTimeDeserializer(dateTimeFormatter)); |
| | | new LocalDateTimeDeserializer(dateTimeDeserializerFormatter)); |
| | | module.addSerializer(LocalDateTime.class, |
| | | new LocalDateTimeSerializer(dateTimeFormatter)); |
| | | // LocalDate:支持 yyyy-MM-dd |