SpringBoot 2.0整合jackson配置日期格式化和反序列化

網上雜七雜八的說法不一,大多數都是抄來抄去,沒有實踐,近期在項目頻繁遇到boot+jackson處理日期的問題,故開此貼。

首先是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>

 

而後是yml文件

固然yml這東西不少人不喜歡,我也寫了個properties版本的)java

spring:
    jackson:
       #參數意義:
       #JsonInclude.Include.ALWAYS              默認
       #JsonInclude.Include.NON_DEFAULT     屬性爲默認值不序列化
       #JsonInclude.Include.NON_EMPTY         屬性爲 空(」」) 或者爲 NULL 都不序列化
       #JsonInclude.Include.NON_NULL           屬性爲NULL   不序列化
        default-property-inclusion: ALWAYS
        time-zone: GMT+8
        date-format: yyyy-MM-dd HH:mm:ss

上面配置對應的properties文件版本:web

#jackson相關配置
spring.jackson.date-format = yyyy-MM-dd HH:mm:ss
#時區必需要設置
spring.jackson.time-zone= GMT+8
#ALWAYS的意思是即時屬性爲null,仍然也會輸出這個key
spring.jackson.default-property-inclusion=ALWAYS

 

而後來定義一個Controller和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 {


    /**
     * 測試時間序列化, java.util.date 類型 -> 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;
    }

    /**
     * 測試時間反序列化 String ->  java.util.date 類型
     */
    @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;

/**
 * 一個model,裏面帶一個日期類型
 */
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;
    }
}

 

啓動主類:

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);
    }
}

 

測試:

試一下,首先是日期序列化, 請求一下試試 

而後是反序列化,也請求一個試試:

相關文章
相關標籤/搜索