新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容主要讲解“SpringBoot-MVC RequestBody中LocalDateTime的自适应配置方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot-MVC RequestBody中LocalDateTime的自适应配置方法”吧!
创新互联建站-专业网站定制、快速模板网站建设、高性价比青山湖网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式青山湖网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖青山湖地区。费用合理售后完善,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的自适应配置方法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!