溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn)

發(fā)布時(shí)間:2020-08-20 06:49:02 來(lái)源:腳本之家 閱讀:415 作者:滄海一刀 欄目:編程語(yǔ)言

網(wǎng)上雜七雜八的說(shuō)法不一,大多數(shù)都是抄來(lái)抄去,沒(méi)有實(shí)踐,近期在項(xiàng)目頻繁遇到boot+jackson處理日期的問(wèn)題,故開此貼。

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

當(dāng)然yml這東西很多人不喜歡,我也寫了個(gè)properties版本的)

spring:
  jackson:
    #參數(shù)意義:
    #JsonInclude.Include.ALWAYS       默認(rèn)
    #JsonInclude.Include.NON_DEFAULT   屬性為默認(rèn)值不序列化
    #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

上面配置對(duì)應(yīng)的properties文件版本:

#jackson相關(guān)配置
spring.jackson.date-format = yyyy-MM-dd HH:mm:ss
#時(shí)區(qū)必須要設(shè)置
spring.jackson.time-zone= GMT+8
#ALWAYS的意思是即時(shí)屬性為null,仍然也會(huì)輸出這個(gè)key
spring.jackson.default-property-inclusion=ALWAYS

然后來(lái)定義一個(gè)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 {


  /**
   * 測(cè)試時(shí)間序列化, 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;
  }

  /**
   * 測(cè)試時(shí)間反序列化 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;

/**
 * 一個(gè)model,里面帶一個(gè)日期類型
 */
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;
  }
}

啟動(dòng)主類:

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

測(cè)試:

試一下,首先是日期序列化, 請(qǐng)求一下試試 

SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn)

然后是反序列化,也請(qǐng)求一個(gè)試試:

SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn)

SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI