服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - SpringBoot2.0整合jackson配置日期格式化和反序列化的实现

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

2021-06-17 11:17沧海一刀 Java教程

这篇文章主要介绍了SpringBoot2.0整合jackson配置日期格式化和反序列化的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

网上杂七杂八的说法不一,大多数都是抄来抄去,没有实践,近期在项目频繁遇到boot+jackson处理日期的问题,故开此贴。

首先是pom

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?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版本的)

?
1
2
3
4
5
6
7
8
9
10
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文件版本:

?
1
2
3
4
5
6
#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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;
  }
}

启动主类:

?
1
2
3
4
5
6
7
8
9
10
11
12
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);
  }
}

测试:

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

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

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

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

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

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

原文链接:https://my.oschina.net/u/2338224/blog/2938032

延伸 · 阅读

精彩推荐