In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "Jackson date formatting method in SpringBoot", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Jackson date formatting method in SpringBoot" article.
Jackson date formatting skills
When using Spring Boot, you need to use Jackson to handle some JSON serialization of type Java Time API, and when dealing with fields of some classes, you can specify their formatting style by directly annotating the properties. Yesterday, however, my colleague encountered a problem with formatting Map data, so that the problem of formatting style could not be solved by annotating.
After all kinds of searches and attempts on the Internet, I finally solved this problem and recorded it for a rainy day.
Talk less and go straight to the code:
Package com.diguage.demo.config;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.util.StdDateFormat;import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration Import org.springframework.context.annotation.Primary;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.time.LocalDate;import java.time.LocalDateTime;import static com.fasterxml.jackson.databind.SerializationFeature.*;import static java.time.format.DateTimeFormatter.ofPattern / * configuration class * * @ author D Guago https://www.diguage.com * / @ Configurationpublic class Config {/ * create ObjectMapper object, configure date format * * @ author D Guago https://www.diguage.com * / @ Bean @ Primary public ObjectMapper objectMapper () {ObjectMapper mapper = new ObjectMapper () String dateTimepattern = "yyyy-MM-dd HH:mm:ss"; String datePattern = "yyyy-MM-dd"; DateFormat dateFormat = new SimpleDateFormat (dateTimepattern); mapper.setDateFormat (dateFormat); mapper.configure (WRITE_DATES_AS_TIMESTAMPS, false); mapper.setDateFormat (new StdDateFormat (). WithColonInTimeZone (true)); JavaTimeModule javaTimeModule = new JavaTimeModule () JavaTimeModule.addDeserializer (LocalDate.class, new LocalDateDeserializer (ofPattern (datePattern); javaTimeModule.addSerializer (LocalDate.class, new LocalDateSerializer (ofPattern (datePattern); javaTimeModule.addDeserializer (LocalDateTime.class, new LocalDateTimeDeserializer (ofPattern (dateTimepattern); javaTimeModule.addSerializer (LocalDateTime.class, new LocalDateTimeSerializer (ofPattern (dateTimepattern); mapper.registerModule (javaTimeModule) Return mapper;}} follow-up questions
I wonder how it will behave when you specify a date formatting style in this way when dealing with fields annotated in a formatted style. Have a chance to test it.
Supplement: Jackson unified configuration date conversion format
Method 1: configure in the configuration file yml
Spring: jackson: default-property-inclusion: ALWAYS time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss
After this serialization, the Date type is formatted into the format in the configuration.
Method 2: configure and create JacksonConfig.java in configuration class
@ Configurationpublic class JacksonConfig {@ Bean @ Order (Ordered.HIGHEST_PRECEDENCE) public Jackson2ObjectMapperBuilderCustomizer customJackson () {return new Jackson2ObjectMapperBuilderCustomizer () {@ Override public void customize (Jackson2ObjectMapperBuilder builder) {builder.serializerByType (LocalDateTime.class, new LocalDateTimeSerializer (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss") Builder.serializerByType (LocalDate.class, new LocalDateSerializer (DateTimeFormatter.ofPattern ("yyyy-MM-dd")); builder.serializerByType (LocalTime.class, new LocalTimeSerializer (DateTimeFormatter.ofPattern ("HH:mm:ss") Builder.deserializerByType (LocalDateTime.class, new LocalDateTimeDeserializer (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss")); builder.deserializerByType (LocalDate.class, new LocalDateDeserializer (DateTimeFormatter.ofPattern ("yyyy-MM-dd") Builder.deserializerByType (LocalTime.class, new LocalTimeDeserializer (DateTimeFormatter.ofPattern ("HH:mm:ss"); builder.serializationInclusion (JsonInclude.Include.NON_NULL); builder.failOnUnknownProperties (false); builder.featuresToDisable (SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);}} }} the above is about the content of this article on "the method of formatting Jackson dates in SpringBoot". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.