看到项目中的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 ...
随机推荐
- Typescript 回调函数、事件侦听的类型定义与注释--拾人牙慧
实际项目中会运到的 Typescript 回调函数.事件侦听的类型定义,如果刚碰到会一脸蒙真的,我就是 这是第一次我自己对 Typescript 记录学习,所以得先说一下我与 Typescript 的 ...
- 【SDOI2015】寻宝游戏
代码 (树链剖分) #include<cstdio> #include<set> using namespace std; typedef long long LL; cons ...
- .Net 调用 sqlserver 存储过程实例
1.输出datatable 存储过程: create proc inparamS @inpar varchar(20) as begin select top 1 * from Address whe ...
- Docker安装部署Mysql8(以作数据持久化)
1.创建容器并进行持久化处理 #拉取镜像 docker pull mysql:8.0.20 #启动镜像,用于拷贝配置文件到宿主机 docker run -p 3306:3306 --name mysq ...
- CSS3移动动画
transition: .3s all ease; .tmall .tmall-tabbodys { width: 100%; position: absolute; left: 0px; trans ...
- 【深入浅出 Yarn 架构与实现】4-6 RM 行为探究 - 申请与分配 Container
本小节介绍应用程序的 ApplicationMaster 在 NodeManager 成功启动并向 ResourceManager 注册后,向 ResourceManager 请求资源(Contain ...
- Git介绍下载安装以及基本使用
目录 一.git介绍 二.下载安装git软件 三.基本使用 四.制作忽略文件 五.Git.Gitee.GitHub.Gitlab.bitbucket的区别 六.基础代码操作分类 一.git介绍 git ...
- C语言学习--指针大小端
// 大端存储: 数据的高位存储在内存的低地址位置 //数据0x12345678, 四字节地址0x0, 0x1,0x2,0x3 //存储方式: 0x0: 存储12, 0x1:存34 0x2: 存5 ...
- 第五周作业-N67044-张铭扬
1. 搭建chrony服务完成多个主机的时间同步. [root@centos8 ~]# yum -y install chrony [root@centos8 ~]# vim /etc/chrony ...
- 四点DLT (Direct Linear Transformation) 算法
\(\mathrm{x}_{i}\) 表示变化前的齐次坐标 \(\mathbf{x}_{i}^{\prime}\) 表示变化后的齐次坐标 我们需要求到一个 \(3\times3\) 的变换矩阵 \(\ ...