看到项目中的DateTimeFormat和JsonFormat就头大
刚来这家公司的时候, 发现很多同事还在用这种方式写代码

当时以为是偶然, 刚才在群里发现还有好多人在交流应当加哪些注解, 声明时区问题.
当写一个东西感到麻烦的时候, 那么大概率是有低成本的更优解的
分以下两种情况来处理
请求体
使用Jackson, 仅保留时间相关配置
package kim.nzxy.ly.common.config;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* JSON相关
*
* @author ly-chn
*/
@Configuration
public class JacksonConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return builder -> {
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(dtf))
.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(dtf))
// 以下代码"可以, 但没必要"
.serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ISO_LOCAL_TIME))
.deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ISO_LOCAL_TIME))
.serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE))
.deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_LOCAL_DATE))
};
}
}
请求行
用于query string / request parameter
package kim.nzxy.ly.common.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.time.format.DateTimeFormatter;
/**
* request parameter date time format
* @author ly-chn
*/
@Configuration
public class DateTimeFormatConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateTimeFormatter(dateTimeFormatter);
// 以下代码"可以, 但没必要"
registrar.setDateFormatter(DateTimeFormatter.ISO_LOCAL_DATE);
registrar.setTimeFormatter(DateTimeFormatter.ISO_LOCAL_TIME);
registrar.registerFormatters(registry);
}
}
yml方式配置, 任选其一即可
spring:
mvc:
format:
date-time: yyyy-MM-dd HH:mm:ss
# 以下代码"可以, 但没必要"
date: yyyy-MM-dd
time: HH:mm:ss
使用示例
实体类
package kim.nzxy.ly.test.entity;
import lombok.Data;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@Data
public class Temp {
private LocalDateTime ldt;
private LocalDate ld;
private LocalTime lt;
}
controller中接收
package kim.nzxy.ly.controller;
import kim.nzxy.ly.modules.system.entity.Temp;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@RestController
@RequestMapping("temp")
public class TempController {
@GetMapping("1")
public Temp test1(LocalDateTime ldt, LocalDate ld, LocalTime lt) {
return new Temp(ldt, ld, lt);
}
@GetMapping("2")
public Temp test2(Temp temp) {
return temp;
}
@PostMapping("3")
public Temp test3(@RequestBody Temp temp) {
return temp;
}
}
test.rest
### 逐个传输
GET http://localhost:8937/temp/1?ldt=2006-01-02 15:04:05&ld=2006-01-02<=15:04
### 实体类接收
GET http://localhost:8937/temp/2?ldt=2006-01-02 15:04:05&ld=2006-01-02<=15:04
### json请求体
POST http://localhost:8937/temp/3
Content-Type: application/json
{
"ldt": "2006-01-02 15:04:05",
"ld": "2006-01-02",
"lt": "15:04"
}
总结
LocalTime和LocalDate的格式一般不用配置, 默认都是常用格式
DateTimeFormatter.ISO_LOCAL_TIME和DateTimeFormatter.ofPattern(String pattern)区别
这个可以查看其源码, ISO_LOCAL_TIME是可以解析"03:04" / "03:04:05" / "03:04:05.111222333"(时分, 时分秒, 时分秒+毫秒)这些格式的, 如果直接使用ofPattern则不行
Date类型怎么处理
首先不建议使用Date类型, 如果有需要, 尽量改成Local*, 除非后端只接受同一种日期格式:
yml配置参考上方yml方式配置
请求行配置尝试新建配置类, 继承WebBindingInitializer, 然后registerCustomEditor
Jackson yml
jackson:
date-format: yyyy-MM-dd HH:mm:ss
# Asia/Shanghai(CST)
time-zone: GMT+8
看到项目中的DateTimeFormat和JsonFormat就头大的更多相关文章
- 项目中整合第三方插件与SpringMVC数据格式化关于ip地址
一.Bootstrap 响应式按钮 <div calss="col-sm-2"> <button class="btn btn-default btn- ...
- Spring Boot项目中使用 TrueLicense 生成和验证License(服务器许可)
一 简介 License,即版权许可证,一般用于收费软件给付费用户提供的访问许可证明.根据应用部署位置的不同,一般可以分为以下两种情况讨论: 应用部署在开发者自己的云服务器上.这种情况下用户通过账号登 ...
- 从项目中理解SSM框架
我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教学课堂中,也会把SSH作为最核心的教学内容. 但是,我们在实际应用中发现,SpringMVC可以完全替代Struts,配 ...
- LocalDateTime在项目中的使用(LocalDateTime对接前端通过时间戳互转、LocalDateTime对接数据库)
目录 1. 博客编写背景 2. LocalDateTime 前端交互 2.1 LocalDateTime 向前端写入时间戳 2.1.1 fastJson 默认的写入格式 2.1.2 更改 fastJs ...
- VS项目中使用Nuget还原包后编译生产还一直报错?
Nuget官网下载Nuget项目包的命令地址:https://www.nuget.org/packages 今天就遇到一个比较奇葩的问题,折腾了很久终于搞定了: 问题是这样的:我的解决方案原本是好好的 ...
- ABP项目中使用Swagger生成动态WebAPI
本文是根据角落的白板报的<使用ABP实现SwaggerUI,生成动态webapi>一文的学习总结,感谢原文作者角落的白板报. 1 安装Swashbuckle.core 1.1 选择WebA ...
- iOS 之项目中遇到的问题总结
昨天去一家公司面试,面试官问了我在项目开发中遇到过哪些问题,是什么引起的,怎样解决的? 当时由于有点小紧张只说出了一两点,现在就来好好总结一下. 问题: 1.两表联动 所谓的两表联动就是有左右两个表格 ...
- My97DatePicker时间控件在项目中的应用
一.下载My97DatePicker的压缩包My97DatePicker.rar,解压. 注:My97DatePicker最新版本有开发包,项目中使用时删掉,以便节省空间,提高程序的运行效率. 二.在 ...
- 在项目中同时使用Objective-C和Swift
苹果发布的Swift语言可以和之前的Objective-C语言同时存在于一个项目中. 可能有人会认为是同一个类文件中既可以有Objective-C也可以有Swift,这是不对的.同一个类文件或同一个代 ...
- 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持
在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...
随机推荐
- JZOJ 3232. 【佛山市选2013】排列
题目 解析 很神奇的一道题 显然,对于一种排列,相当于给出了数字 \(1..n\) 的对应关系,且不重复不遗漏,刚好把 \(1\) 到 \(n\) 又包含了一遍. 对,连边! 每个数向它对应的数连边, ...
- Prime Distance
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- 英国学者在真实世界早期RA队列研究中发现较高比例的临床缓解患者仍存在能量多普勒超声活性
标签: 类风湿关节炎; 目标治疗策略; 能量多普勒活性; 预测因子 英国学者在真实世界早期RA队列研究中发现较高比例的临床缓解患者仍存在能量多普勒超声活性 电邮发布日期:2016年4月6日 本研究的重 ...
- Atcoder题解:Arc156_c
数据范围 \(10^5\),但是介绍一个 \(O(n\log n)\) 做法. 我们考虑观察样例,发现样例都很小,而且 \(\text{LCS}\) 的长度都是 \(1\),那么我们就猜答案最多为 \ ...
- Socket.io + Knex 实现私聊聊天室
前言 本文只介绍实现的核心代码,目的是记录和分享知识.若感兴趣可以往下看,在文章最后贴上了仓库地址.前端使用 Vite + Vue3:后端使用 Knex + Express. Room 的概念 私密 ...
- PostGIS之空间连接
1. 概述 PostGIS 是PostgreSQL数据库一个空间数据库扩展,它添加了对地理对象的支持,允许在 SQL 中运行空间查询 PostGIS官网:About PostGIS | PostGIS ...
- GPS地图生成01之概述
图片来源: Author: Tang Email: jianbo.tang@csu.edu.cn
- jQuery使用与语法
jQuery安装 1.从官网Download jQuery | jQuery下载安装: 2.CDN在线加载: statistic CDN:https://cdn.staticfile.org/jque ...
- SpringMVC学习笔记【狂神说】
1.MVC是什么 MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范. 是将业务逻辑.数据.显示分离的方法来组织代码. MVC主要作用是降低了视图与 ...
- 新发现的几个不错的c++库
1.coost 包含了各种常用的库,比boost轻量级的基于c++11的库 https://github.com/idealvin/coost 2.ImGui 一个较少依赖的gui界面库 https: ...