SpringBoot2.0整合jackson配置日期格式化和反序列化的实现
作者:沧海一刀 发布时间:2022-06-29 14:13:52
标签:SpringBoot2,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);
}
}
测试:
试一下,首先是日期序列化, 请求一下试试
然后是反序列化,也请求一个试试:
来源:https://my.oschina.net/u/2338224/blog/2938032


猜你喜欢
- CountDownTimerCountDownTimer 是android 自带的一个倒计时类,使用这个类可以很简单的实现 倒计时功能Cou
- 本文实例讲述了C#实现对文件进行加密解密的方法。分享给大家供大家参考。具体如下:using System;using System.IO;u
- 1、实现循环队列【OJ链接】循环队列一般通过数组实现。我们需要解决几个问题。(1)数组下标实现循环a、下标最后再往后(offset 小于 a
- 本文实例讲述了Java Swing实现让窗体居中显示的方法。分享给大家供大家参考,具体如下:Swing组件是AWT组建的增强组件,是功能强大
- 记得在 MS Build 2020 大会上,C# 语言开发项目经理 Mads Torgersen 宣称 C# 9.0 将会随着 .NET 5
- 背景WebView是在APP中,可以很方便的展示web页面,并且与web交互APP的数据。方便,并且更新内容无需APP发布新版本,只需要将最
- 基本结构我们先来看一段最基本的代码,分析这段代码在RxJava中是如何实现的。Observable.OnSubscribe<Strin
- 1 修饰类和方法package site.exciter.springmvc.handlers;import org.springframe
- 组合模式是一种常见的设计模式(但我感觉有点复杂)也叫合成模式,有时又叫做部分-整体模式,主要是用来描述部分与整体的关系。个人理解:组合模式就
- 今天一个读者问我关于Android通过调用Webservice实现天气预报这篇文章的源码下载后出现的错误Could not find cla
- 目录1. 前言2. 方法1 使用SurfaceView3. 方法2 给拍照下来的图片添加水印第一步:获取拍照权限第二步:拍照调用相机进行拍照
- 在 Intellij Idea 中,我们需要设置 Settings 中的 Java Compiler 和 Project Structure
- 最近由于参加一个小小的创意比赛,用安卓做了一个小小的图片识别应用,主要是通过拍照识别图片中的菜品,还有对象位置查找的东西。之前没有做过安卓,
- Android的PopupWindow是个很有用的widget,利用它可以实现悬浮窗体的效果,比如实现一个悬浮的菜单,最常见的应用就是在视频
- 本文实例讲述了C#线程同步的三类情景,分享给大家供大家参考。具体分析如下:C# 已经提供了我们几种非常好用的类库如 BackgroundWo
- 在Android实现没有标题栏的方法有两种:在代码中添加requestWindowFeature(Window.FEATURE_NO_TIT
- 前言:小编引入的图片和文字描述都是来自于尚硅谷的视频讲解,在此感谢尚硅谷的老师,同时也结合 seata文档官方文档进行整合项目地址(gite
- Failed to execute goal org.apache.maven.plugins:maven-resources-plugin
- 判断JSONObject是否存在某个KeyJSONObject jsonObj = new JSONObject();jsonObj.put
- 启动协程的基本方式1.GlobalScope.launch代码示例:fun testGlobalScope() {