Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of Jackson date format in SpringBoot

Shulou Source: shulou.com Published: 2022-05-31 22:40:35 09月13日 Update

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.

Tags: Format configuration date content question way style method comment processing field sequence article knowledge article type emergency code value available through Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info Docker Shulou Information Shulou Technology