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版本的)
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); } }
测试:
试一下,首先是日期序列化, 请求一下试试
然后是反序列化,也请求一个试试:
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
记nodejs在pm2下使用log4js cluster模式的日志打印丢失问题
我使用pm2的cluster集群模式管理node服务,使用log4js打印日志。最近公司业务量上升,与此同时问题订单也随之增多。但多次在排查异常订单时找不到订单的正常日志,在基本排除程序本身问题后我将目光投在了日志打印插件log4js身上。 1:这是我目前的log4js的配置 log4js.configure({ appenders: { out: { type: 'stdout' }, info: { type: 'dateFile', filename: 'info', pattern: '-yyyy-MM-dd.log', layout: { type: 'pattern', pattern: '%d{yyyy-MM-ddThh:mm:ss.SSSO} %p %c - [%m]' } }, }, categories: { default: { appenders: ['out','info'], level: 'all' }, }, pm2: true, disableClustering: true }); 我在网上也找到了很多博客,...
- 下一篇
你的 Ubuntu 为何如此炫酷?
很多人觉得 Windows 是最好的操作系统,它的用户图形化交互界面、它的各种功能应用,还有它的易用性等方面完全就碾压一切,Linux 党对此表示不屑;又有很多人把 elementary OS 定义为最美的 Linux 发行版,Ubuntu 党跳出来对此表示不屑。啊~并不是想(打)引(去)战(吧),其实笔者觉得不管是 Windows 还是 Linux,不管是 Ubuntu 还是其它 Linux 发行版,都有自己的特点,至于当下为什么更多的人使用 Windows 而不是 Linux,其中一个大原因在于易用性和相应的配套应用与个性化设置,然而其实他们误会了已经发展到如今的 Linux 了,或者说有些人根本没接触过吧。 Linux 发展到今天,早已不再是只局限于服务器与嵌入式端了,作为个人日常使用的操作系统也完全是杠杠的。鉴于笔者是 Ubuntu 流,本文就以目前比较活跃的 LTS Ubuntu 18.04 为例,从系统安装到具体配置,再到如何个性化美化,一步步分享一下个人经验,让大家了解到 Linux 也可以像 Windows 那样简易;而对于 Ubuntu 党外人士,只想告诉他们,Ubu...
相关文章
文章评论
共有0条评论来说两句吧...