18

SpringBoot时间参数处理完整解决方案

 4 years ago
source link: http://www.eknown.cn/index.php/spring-boot/params-time.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在JavaWeb程序的开发过程中,接口是前后端对接的主要窗口,而接口参数的接收有时候是一个令人头疼的事情,这其中最困扰程序猿的,应该是时间参数的接收。

比如:设置一个用户的过期时间,前端到底以什么格式传递参数呢?时间戳?还是2019-12-01 22:13:00这种格式?还是其他格式?

今天我就来总结一下SpringBoot Web应用接口接收时间类型参数的问题解决方案。

注:目前我对Spring源码的掌握还不是很好,所以这一篇仅仅总结一下解决方法,后面感悟多了会重写一下!:sunglasses:

示例代码请前往: https://github.com/laolunsi/spring-boot-examples

经过简单的测试,我们知道:

  1. 不使用@RequestBody注解的情况下,所有时间类型参数都会引起报错;
  2. 使用@RequestBody,前端传递时间戳或 2019-11-22 形式正常,传递 2019-11-22 11:22:22 报错,其他格式同样报错。

之前有接触过类似的解决办法,在类的属性上加上@DateFormat注解,解决单个时间参数问题。

但是局限较多。

理想的解决方案是:一次配置,全局通用,多种格式,自动转换(朗朗上口嗷)

一、源码简要分析

首先我们来简单分析一下源码:

je2MnyM.png!web

深入的就不解释了(我现在也不懂


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK