本篇內容主要講解“SpringBoot-MVC RequestBody中LocalDateTime的自適應配置方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“SpringBoot-MVC RequestBody中LocalDateTime的自適應配置方法”吧!
創(chuàng)新互聯建站-專業(yè)網站定制、快速模板網站建設、高性價比青山湖網站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式青山湖網站制作公司更省心,省錢,快速模板網站建設找我們,業(yè)務覆蓋青山湖地區(qū)。費用合理售后完善,10年實體公司更值得信賴。
請求的json報文中可能會出現 一下幾種:
['2019-01-01','2019-01-01 12:03:34','20190101120334']
但是接收的Request實體類日期字段是LocalDateTime類型
LocalDateTime applyDate;
希望的情況是 :
輸入 yyyy-MM-dd HH:mm:ss 、yyyyMMddHHmmss等精確到秒的格式,可以自動被封裝LocalDateTime;
輸入 yyyy-MM-dd 、yyyyMMdd等精確到日的格式,也可以被封裝成LocalDateTime, HHmmss默認為000000;
默認情況下,是沒辦法 自動轉換的,需要在 ObjectMapper中配置,網上主流的配置如下
@Configuration @Slf4j public class MvcConfig implements WebMvcConfigurer { /** * 更改jackson默認配置 * @return */ @Bean @Primary public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder){ //日期和時間格式化 JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))); javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))); javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); javaTimeModule.addDeserializer(LocalDateTime.class,new MyLocalDateTimeDeserializer()); objectMapper.registerModule(javaTimeModule); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); return objectMapper; } }
但實際效果是,上面第二種情況沒法生效。百度了很多也都是重復內容,無效。google得一答案:
@Configuration @Slf4j public class MvcConfig implements WebMvcConfigurer { /** * 更改jackson默認配置 * @return */ @Bean @Primary public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder){ ObjectMapper objectMapper = builder.json().build(); SimpleModule simpleModule = new SimpleModule(); simpleModule.addDeserializer(LocalDateTime.class,new MyLocalDateTimeDeserializer()); objectMapper.registerModule(simpleModule); //忽視請求報文中未匹配到的字段(多余的字段) objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); return objectMapper; } }
需要自定義反序列化類
MyLocalDateTimeDeserializer
public class MyLocalDateTimeDeserializer extends JsonDeserializer{ @Override public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException { String dateStr = parser.getText(); DateTime dateTime = null; try{ dateTime = DateUtil.parse(dateStr); }catch (Exception e){ dateTime = DateUtil.parseDateTime(dateStr.replaceAll("T"," ")); } Date date = dateTime.toJdkDate(); Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime(); return localDateTime; } @Override public Class> handledType() { return LocalDateTime.class; } }
deserialize方法的邏輯就靠自己實現了,我這邊是借助了hutool工具類進行了簡化。
到此,相信大家對“SpringBoot-MVC RequestBody中LocalDateTime的自適應配置方法”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!