SpringBoot2.0整合jackson配置日期格式化和反序列化的实现
程序员文章站
2024-03-05 09:30:42
网上杂七杂八的说法不一,大多数都是抄来抄去,没有实践,近期在项目频繁遇到boot+jackson处理日期的问题,故开此贴。
首先是pom
网上杂七杂八的说法不一,大多数都是抄来抄去,没有实践,近期在项目频繁遇到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); } }
测试:
试一下,首先是日期序列化, 请求一下试试
然后是反序列化,也请求一个试试:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。