In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the adaptive configuration method of LocalDateTime in SpringBoot-MVC RequestBody". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the adaptive configuration method of LocalDateTime in SpringBoot-MVC RequestBody".
The following may appear in the requested json message:
['090101120334'] 2019-01-01-01: 0112: 2019-01-0112: 03
But the received Request entity class date field is of type LocalDateTime
LocalDateTime applyDate
The hope is that:
Input yyyy-MM-dd HH:mm:ss, yyyyMMddHHmmss and other formats accurate to seconds, and LocalDateTime can be automatically encapsulated.
Input yyyy-MM-dd, yyyyMMdd and other accurate formats to the day, can also be encapsulated as LocalDateTime, HHmmss default is 000000
By default, there is no way to automatically convert, and it needs to be configured in ObjectMapper. The mainstream configurations on the Internet are as follows
@ Configuration@Slf4jpublic class MvcConfig implements WebMvcConfigurer {/ * change jackson default configuration * @ return * / @ Bean @ Primary public ObjectMapper objectMapper (Jackson2ObjectMapperBuilder builder) {/ / date and time format 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;}}
But the actual effect is that the second situation above does not work. Baidu has a lot of repetitive content, which is invalid. Google has an answer:
@ Configuration@Slf4jpublic class MvcConfig implements WebMvcConfigurer {/ * change jackson default configuration * @ 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) / / ignore the unmatched fields in the request message (redundant fields) objectMapper.configure (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure (SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); return objectMapper;}}
Need to customize deserialization class
MyLocalDateTimeDeserializerpublic 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;}}
The logic of the deserialize method is implemented on my own, and I simplified it with the help of the hutool utility class.
At this point, I believe you have a deeper understanding of "the adaptive configuration method of LocalDateTime in SpringBoot-MVC RequestBody". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.