SpringBoot2.0整合jackson配置日期格式化和反序列化的实现

 更新时间:2018年11月26日 14:58:53   作者:沧海一刀  
这篇文章主要介绍了SpringBoot2.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版本的)

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文件版本:

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

测试:

试一下,首先是日期序列化, 请求一下试试 

然后是反序列化,也请求一个试试:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • java密钥交换算法DH定义与应用实例分析

    java密钥交换算法DH定义与应用实例分析

    这篇文章主要介绍了java密钥交换算法DH定义与应用,结合实例形式分析了Java密钥交换算法DH的原理、定义、使用方法及相关操作注意事项,需要的朋友可以参考下
    2019-09-09
  • MyBatis的JdbcType与Oracle、MySql数据类型一览表

    MyBatis的JdbcType与Oracle、MySql数据类型一览表

    这篇文章主要介绍了MyBatis的JdbcType与Oracle、MySql数据类型一览表,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • java实现百度云文字识别接口代码

    java实现百度云文字识别接口代码

    这篇文章主要为大家详细介绍了java实现百度云文字识别的接口代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-11-11
  • 详解Struts2动态方法调用

    详解Struts2动态方法调用

    这篇文章主要介绍了详解Struts2动态方法调用,涉及调用方法的代码,具有一定参考价值,需要的朋友可以了解下。
    2017-09-09
  • Mybatis如何解决sql中like通配符模糊匹配问题

    Mybatis如何解决sql中like通配符模糊匹配问题

    这篇文章主要介绍了Mybatis如何解决sql中like通配符模糊匹配问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • 100行java写的微信跳一跳辅助程序

    100行java写的微信跳一跳辅助程序

    本篇文章给大家分享了用java写的一个微信跳一跳辅助脚本程序,有兴趣的朋友参考学习下。
    2018-01-01
  • 基于JDK动态代理原理解析

    基于JDK动态代理原理解析

    这篇文章主要介绍了基于JDK动态代理原理解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • 浅谈Spring 中 @EnableXXX 注解的套路

    浅谈Spring 中 @EnableXXX 注解的套路

    本文主要介绍了浅谈Spring 中 @EnableXXX 注解的套路,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • Spring Cloud 专题之Sleuth 服务跟踪实现方法

    Spring Cloud 专题之Sleuth 服务跟踪实现方法

    这篇文章主要介绍了Spring Cloud 专题之Sleuth 服务跟踪,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-08-08
  • MySQL和Java通用加密解密方式小结

    MySQL和Java通用加密解密方式小结

    这篇文章主要介绍了MySQL和Java通用加密解密方式,加密方式使用AES加密,在转成Base64,本文结合实例代码给大家介绍的非常详细,需要的朋友参考下吧
    2023-12-12

最新评论