SpringBoot2.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);
  }
}

测试:

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

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

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

(0)

相关推荐

  • SpringBoot之LogBack配置详解

    LogBack 默认集成在 Spring Boot 中,是基于 Slf4j 的日志框架.默认情况下 Spring Boot 是以 INFO 级别输出到控制台. 它的日志级别是: ALL < TRACE < DEBUG < INFO < WARN < ERROR < OFF 配置 LogBack 可以直接在 application.properties 或 application.yml 中配置,但仅支持一些简单的配置,复杂的文件输出还是需要配置在 xml 配置文件中.配

  • Spring Boot配置拦截器及实现跨域访问的方法

    拦截器功能强大,能够深入方法前后,常应用于日志记录.权限检查和性能检测等,几乎是项目中不可或缺的一部分,本文就来实现Spring Boot自定义拦截器的配置. 理论指导 问:Spring Boot怎么配置拦截器? 答:配置一个拦截器需要两步完成. 自定义拦截器,实现HandlerInterceptor这个接口.这个接口包括三个方法,preHandle是请求执行前执行的,postHandler是请求结束执行的,但只有preHandle方法返回true的时候才会执行,afterCompletion是

  • Spring Boot2配置服务器访问日志过程解析

    这篇文章主要介绍了Spring Boot2配置服务器访问日志过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Tomcat控制台中看到的日志是服务器的日志,而服务器访问日志则是记录服务处理的请求信息. 开发环境:IntelliJ IDEA 2019.2.2 Spring Boot版本:2.1.8 1.新建一个名称为demo的Spring Boot项目. 2.application.yml 添加配置 server: tomcat: base

  • SpringBoot通过yml和xml文件配置日志输出方法

    SpringBoot中默认使用Logback进行日志输出,可以同时使用SpringBoot框架的配置文件application.yml或是通过logback的配置文件logback.xml进行配置. 通过application.yml配置 <?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!--定义日志文件的存储地址 勿在 Lo

  • Springboot引用外部配置文件的方法步骤

    现在的项目越来越多的都是打包成jar运行尤其是springboot项目,这时候配置文件如果一直放在项目中,每次进行简单的修改时总会有些不方便,这里我们看下打包成jar之后,从外部配置文件中读取配置信息. 首先想到的是通过java代码读取外边某个路径下的文件,但是开始做之后发现好多问题.后来又找其它解决方案,正好搜到一种简单的解决方式: java -jar demo.jar --Dspring.config.location=myapplication.properties 这样就可以通过@val

  • springboot多环境(dev、test、prod)配置详解

    我们在开发应用的时候,通常同一套程序会被应用和安装到几个不同的环境中,比如开发.测试.生产等. 其中每个环境的数据库地址.服务器端口等配置都不同.如果在为不同环境打包时都要频繁的修改配置文件,那必将是个非常繁琐的且容易出错的事情. 对于多环境的配置,各种项目构建工具或是架构的基本思路是一样的,通过配置多份不同的环境配置文件,在通过打包命令指定需要打包的内容之后 进行区分打包,spring boot也不列外.或者说实现起来更加简单. propertiest配置格式 在Spring Boot中多环境

  • Spring Boot实现邮件服务(附:常见邮箱的配置)

    前言 发送邮件应该是网站的必备功能之一,什么注册验证,忘记密码或者是给用户发送营销信息.最早期的时候我们会使用JavaMail相关api来写发送邮件的相关代码,后来spring退出了JavaMailSender更加简化了邮件发送的过程,在之后springboot对此进行了封装就有了现在的spring-boot-starter-mail,本文将详细给大家介绍了关于Spring Boot邮件服务的相关内容,下面话不多说了,来一起看看详细的介绍吧 1. pom.xml文件中引入依赖 <dependen

  • SpringBoot使用编程方式配置DataSource的方法

    Spring Boot使用固定算法来扫描和配置DataSource.这使我们可以在默认情况下轻松获得完全配置的DataSource实现. Spring Boot还会按顺序快速的自动配置连接池(HikariCP, Apache Tomcat或Commons DBCP),具体取决于路径中的哪些类. 虽然Spring Boot的DataSource自动配置在大多数情况下运行良好,但有时我们需要更高级别的控制,因此我们必须设置自己的DataSource实现,因此忽略自动配置过程. Maven依赖 总体而

  • Spring Boot应用配置常用相关视图解析器详解

    SpringBoot的自动装配装配了视图解析器了吗? 我们可以看到SpringBoot自动装配的WebMvcAutoConfiguration类中,装配了以下关于ViewResolver(视图解析器)的类.可以看到SpringBoot已经自动装配了InternalResourceViewResolver类,又是通过外部资源配置的方式来配置此视图解析器this.mvcProperties.getView().getPrefix(),所以我们可以在application.properties文件配置

  • SpringBoot2.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

  • springboot2.0整合logback日志的详细代码

    一. 近期自己的项目想要一个记录日志的功能,而springboot本身就内置了日志功能,然而想要输入想要的日志,并且输出到磁盘,然后按天归档,或者日志的切分什么的,自带的日志仅仅具有简单的功能,百度了一番,总结如下,适合大多数的应用场景 二. springboot的pom文件都会引一个parent <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-star

  • SpringBoot2.0 整合 SpringSecurity 框架实现用户权限安全管理方法

    一.Security简介 1.基础概念 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring的IOC,DI,AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为安全控制编写大量重复代码的工作. 2.核心API解读 1).SecurityContextHolder 最基本的对象,保存着当前会话用户认证,权限,鉴权等核心数据.Secu

  • SpringBoot2.0 整合 Dubbo框架实现RPC服务远程调用方法

    一.Dubbo框架简介 1.框架依赖 图例说明: 1)图中小方块 Protocol, Cluster, Proxy, Service, Container, Registry, Monitor 代表层或模块,蓝色的表示与业务有交互,绿色的表示只对 Dubbo 内部交互. 2)图中背景方块 Consumer, Provider, Registry, Monitor 代表部署逻辑拓扑节点. 3)图中蓝色虚线为初始化时调用,红色虚线为运行时异步调用,红色实线为运行时同步调用. 4)图中只包含 RPC

  • SpringBoot2.0整合WebSocket代码实例

    这篇文章主要介绍了SpringBoot2.0整合WebSocket代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 之前公司的某个系统为了实现推送技术,所用的技术都是Ajax轮询,这种方式浏览器需要不断的向服务器发出请求,显然这样会浪费很多的带宽等资源,所以研究了下WebSocket,本文将详细介绍下. 一.什么是WebSocket? WebSocket是HTML5开始提供的一种在单个TCP连接上进行全双工通讯的协议,能更好的节省服务器资

  • SpringBoot2.0整合tk.mybatis异常解决

    pom配置如下(标准简易版): <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <properties> <java.version>1.8&

  • springboot2.0整合dubbo的示例代码

    写在前面: 使用springboot作为web框架,方便开发许多,做分布式开发,dubbo又不可少,那么怎么整合在一起呢, 跟我学一遍,至少会用 注意,springboot2.0和springboot1.x与dubbo整合不一样, 1.环境 1.新建一个空的maven项目,作为父工程,新建moudle,,service(接口层,及实现层,没有具体分,),web(web层,springboot项目) 项目结构如下 父pom如下 <properties> <project.build.sou

  • SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解决方案

    hystrix参数使用方法 通过注解@HystrixCommand的commandProperties去配置, 如下就是hystrix命令超时时间命令执行超时时间,为1000ms和执行是不启用超时 @RestController public class MovieController { @Autowired private RestTemplate restTemplate; @GetMapping("/movie/{id}") @HystrixCommand(commandPro

  • SpringBoot2.0整合Shiro框架实现用户权限管理的示例

    GitHub源码地址:知了一笑 https://github.com/cicadasmile/middle-ware-parent 一.Shiro简介 1.基础概念 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.作为一款安全框架Shiro的设计相当巧妙.Shiro的应用不依赖任何容器,它不仅可以在JavaEE下使用,还可以应用在JavaSE环境中. 2.核心角色 1)Subject:认证主体 代表当前系统的使用者,就是用户,在Shiro的认证中,

  • SpringBoot中Jackson日期格式化技巧分享

    目录 Jackson 日期格式化技巧 后续问题 补充:Jackson 统一配置 日期转换格式 参考资料 Jackson 日期格式化技巧 使用 Spring Boot 时,需要使用 Jackson 处理一些 Java Time API 类型的 JSON 序列化问题,在处理一些类的字段时,可以通过直接在属性上加注解的方式来指定其格式化样式.但是,昨天同事遇到一个格式化 Map 数据的问题,这样就不能通过加注解来解决格式化样式的问题了. 在网上各种搜索,各种尝试后,终于解决了这个问题,记录一下,以备不

随机推荐