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

网友投稿 250 2023-01-18


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

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

首先是POM

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

io.cj.learning

boot2exam

0.0.1-SNAPSHOT

jar

boot2exam

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

2.0.0.RELEASE

<!-- lookup parent from repository -->

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-data-redis

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-web

RELEASE

compile

org.springframework.boot

spring-boot-starter-web

RELEASE

compile

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

io.cj.learning

boot2exam

0.0.1-SNAPSHOT

jar

boot2exam

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

2.0.0.RELEASE

<!-- lookup parent from repository -->

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-data-redis

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-web

RELEASE

compile

org.springframework.boot

spring-boot-starter-web

RELEASE

compile

org.springframework.boot

spring-boot-maven-plugin

然后是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.setIntPropertihttp://es(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);

}

}

测试:

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

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:企业研发管理平台的公司(研发管理平台软件)
下一篇:包含post测试网的词条
相关文章

 发表评论

暂时没有评论,来抢沙发吧~