使用springboot框架对日期类型进行操作,遇到无法保持的情况,一开始报400的错误(解决方法),解决之后日期类型无法保存到数据库,为了解决这个问题,设置了个全局date转换器。

配置方法

1、新增一个转换类

新增一个string转换成date的类,实现Converter接口,代码如下:

import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils; public class StringToDateConverter implements Converter<String, Date> { private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
private static final String shortDateFormat = "yyyy-MM-dd"; @Override
public Date convert(String value) { if(StringUtils.isEmpty(value)) {
return null;
} value = value.trim(); try {
if(value.contains("-")) {
SimpleDateFormat formatter;
if(value.contains(":")) {
formatter = new SimpleDateFormat(dateFormat);
}else {
formatter = new SimpleDateFormat(shortDateFormat);
} Date dtDate = formatter.parse(value);
return dtDate;
}else if(value.matches("^\\d+$")) {
Long lDate = new Long(value);
return new Date(lDate);
}
} catch (Exception e) {
throw new RuntimeException(String.format("parser %s to Date fail", value));
}
throw new RuntimeException(String.format("parser %s to Date fail", value));
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

2、注册转换器

新增一个类,配置为Configuration,把第一步新增的类注册为转换器,代码如下:

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; @Configuration
public class WebConfigBeans { @Autowired
private RequestMappingHandlerAdapter handlerAdapter; /**
* 增加字符串转日期的功能
*/ @PostConstruct
public void initEditableAvlidation() { ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer)handlerAdapter.getWebBindingInitializer();
if(initializer.getConversionService()!=null) {
GenericConversionService genericConversionService = (GenericConversionService)initializer.getConversionService(); genericConversionService.addConverter(new StringToDateConverter()); } } }

springboot date接收参数的更多相关文章

  1. SpringBoot Controller接收参数的几种方式盘点

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:SpringBoot Controller接收参数的几种方式盘点: SpringBoot Controller接收参数的几种常用方式盘点 ...

  2. SpringBoot Controller接收参数的几种常用方式

    第一类:请求路径参数1.@PathVariable获取路径参数.即url/{id}这种形式. 2.@RequestParam获取查询参数.即url?name=这种形式 例子GET http://loc ...

  3. SpringBoot Controller接收参数的几种常用方

    第一类:请求路径参数 1.@PathVariable 获取路径参数.即url/{id}这种形式. 2.@RequestParam 获取查询参数.即url?name=这种形式 例子 GET http:/ ...

  4. springboot接口 接收参数为实体对象跟MultipartFile对象报错。

    要把文件和普通数据类型分开接口传输,不可以兼容多个类型参数, 建议是传文件一个接口,返回url路径,再和普通数据一起提交,就是两次 企业上的做法都是这样,先用文件服务器保存文件,返回文件路径 http ...

  5. springboot项目--传入参数校验-----SpringBoot开发详解(五)--Controller接收参数以及参数校验----https://blog.csdn.net/qq_31001665/article/details/71075743

    https://blog.csdn.net/qq_31001665/article/details/71075743 springboot项目--传入参数校验-----SpringBoot开发详解(五 ...

  6. Spring MVC接收参数(Map,List,JSON,Date,2个Bean)(记录一次面试惨状)

    题目Spring MVC 接收参数 MapListDate2个BeanJSON Spring MVC接收参数 -Map Spring MVC接收参数 -List Spring MVC接收参数 -dat ...

  7. Springboot接收参数

    接收参数有三个方法. 1.接收id的方法: @RestController public class ControllerTest { //在这里读取配置文件 @Autowired private T ...

  8. 探究Spring Boot中的接收参数问题与客户端发送请求传递数据

    结合此篇参考Spring框架学习笔记(9)--API接口设计相关知识及具体编码实现 在使用Spring Boot进行接收参数的时候,发现了许多问题,之前一直都很忙,最近才稍微有空研究一下此问题. 网上 ...

  9. Struts2学习---基本配置,action,动态方法调用,action接收参数

    首先我们先来直接配置,然后再来讲原理: 第一步:jar包的引入: 我们可以到struts2的官网上下载: http://struts.apache.org/download.cgi#struts251 ...

随机推荐

  1. 111python

    补充一句,为兼容平台文件地址可以这么写: file = os.path.join('dir', 'file_name')

  2. MVC ---- 如何扩展方法

    先定义一个扩展类: public static class StringExtend { //扩展一个string的方法 public static bool IsNullOrEmpty(this s ...

  3. python 二进制数相加

    def add_binary_nums(x,y): max_len = max(len(x), len(y)) x = x.zfill(max_len) y = y.zfill(max_len) re ...

  4. cookie(2)

    转载,原文地址 https://segmentfault.com/a/1190000004743454 一.引言 随着浏览器的处理能力不断增强,越来越多的网站开始考虑将数据存储在「客户端」,那就不得不 ...

  5. kali 下程序卸载方法

    ali中主要为2种卸载方法:1.apt2.dpkg 使用apt的方式有:apt-get remove [package]apt-get remove --purge # ------(package ...

  6. [ios][map]自定义地图标注

    参考:http://blog.csdn.net/mad1989/article/details/8794762 ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高 ...

  7. 团队项目用户验收评审——《WAP团队》

    团队项目用户验收评审——<WAP团队> 1.验收准备的相关文档链接:https://github.com/LVowe999/xiangmubaogao.git                ...

  8. 《剑指offer》第三十六题(二叉搜索树与双向链表)

    // 面试题36:二叉搜索树与双向链表 // 题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求 // 不能创建任何新的结点,只能调整树中结点指针的指向. #include < ...

  9. elementUI和iview兼容么

    听说iview的作者居然是91年的,我要赶快加油了. https://zhuanlan.zhihu.com/p/25739512

  10. Python 错误与异常

    2017-08-01 13:40:17 在程序运行过程中,总会遇到各种各样的错误. 有的错误是程序编写有问题造成的,比如本来应该输出整数结果输出了字符串,这种错误我们通常称之为bug,bug是必须修复 ...