Spring boot 2.0 integrates jackson configuration date formatting and deserialization

Posted by UseeHere on Sun, 08 Dec 2019 06:36:09 +0100

There are many different opinions on the Internet. Most of them are copied back and forth. There is no practice. Recently, in the project, we frequently encountered the problem of boot+jackson processing date, so we opened this post.

The first is POM.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.cj.learning</groupId>
    <artifactId>boot2exam</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>boot2exam</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

 

And then the yml file

(of course, many people don't like yml. I also wrote a property version.)

spring:
    jackson:
       #Parameter meaning:
       #JsonInclude.Include.ALWAYS default
       #Jsoninclude.include.non'default property is the default value and is not serialized
       #Jsoninclude.include.non \ empty property is empty (") or NULL is not serialized
       #Jsoninclude.include.non'NULL property is NULL and not serialized
        default-property-inclusion: ALWAYS
        time-zone: GMT+8
        date-format: yyyy-MM-dd HH:mm:ss

The corresponding property file version is configured above:

#jackson related configuration
spring.jackson.date-format = yyyy-MM-dd HH:mm:ss
#Time zone must be set
spring.jackson.time-zone= GMT+8
#ALWAYS means that even if the property is null, the key will still be output
spring.jackson.default-property-inclusion=ALWAYS

 

Then define a Controller and JAVA Bean

Controller:

package io.cj.learning.boot2exam.controller;

import io.cj.learning.boot2exam.model.DateFormatTest;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping(value="/test")
public class TestController {


    /**
     * Test time serialization, java.util.date type - > string
     * @return
     */
    @RequestMapping(value="/dateFormatTest", method = RequestMethod.GET)
    @ResponseBody
     public DateFormatTest dateFormatTest(){
        DateFormatTest dateFormatTest = new DateFormatTest();
        dateFormatTest.setIntProperties(100);
        dateFormatTest.setDateProperties(new Date());
        return dateFormatTest;
    }

    /**
     * Test time deserialization string - > java.util.date type
     */
    @RequestMapping(value="/dateFormatTest2" ,method = RequestMethod.POST)
    public void dateFormatTest2(@RequestBody DateFormatTest model){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(model.getIntProperties());
        System.out.println(sdf.format(model.getDateProperties()));
        System.out.println(model.getStrProperties());
    }


}

 

Java Bean:

package io.cj.learning.boot2exam.model;

import java.util.Date;

/**
 * A model with a date type
 */
public class DateFormatTest {

    private Integer intProperties;
    private Date dateProperties;
    private String strProperties;

    public Integer getIntProperties() {
        return intProperties;
    }

    public void setIntProperties(Integer intProperties) {
        this.intProperties = intProperties;
    }

    public Date getDateProperties() {
        return dateProperties;
    }

    public void setDateProperties(Date dateProperties) {
        this.dateProperties = dateProperties;
    }

    public String getStrProperties() {
        return strProperties;
    }

    public void setStrProperties(String strProperties) {
        this.strProperties = strProperties;
    }
}

 

Start main class:

package io.cj.learning.boot2exam;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Boot2examApplication {

    public static void main(String[] args) {
        SpringApplication.run(Boot2examApplication.class, args);
    }
}

 

Test:

Try it. First of all, date serialization. Please try it

Then deserialize, and ask for a try:

Topics: Programming Spring Java Maven Apache