springboot date接收参数
使用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接收参数的更多相关文章
- SpringBoot Controller接收参数的几种方式盘点
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:SpringBoot Controller接收参数的几种方式盘点: SpringBoot Controller接收参数的几种常用方式盘点 ...
- SpringBoot Controller接收参数的几种常用方式
第一类:请求路径参数1.@PathVariable获取路径参数.即url/{id}这种形式. 2.@RequestParam获取查询参数.即url?name=这种形式 例子GET http://loc ...
- SpringBoot Controller接收参数的几种常用方
第一类:请求路径参数 1.@PathVariable 获取路径参数.即url/{id}这种形式. 2.@RequestParam 获取查询参数.即url?name=这种形式 例子 GET http:/ ...
- springboot接口 接收参数为实体对象跟MultipartFile对象报错。
要把文件和普通数据类型分开接口传输,不可以兼容多个类型参数, 建议是传文件一个接口,返回url路径,再和普通数据一起提交,就是两次 企业上的做法都是这样,先用文件服务器保存文件,返回文件路径 http ...
- springboot项目--传入参数校验-----SpringBoot开发详解(五)--Controller接收参数以及参数校验----https://blog.csdn.net/qq_31001665/article/details/71075743
https://blog.csdn.net/qq_31001665/article/details/71075743 springboot项目--传入参数校验-----SpringBoot开发详解(五 ...
- Spring MVC接收参数(Map,List,JSON,Date,2个Bean)(记录一次面试惨状)
题目Spring MVC 接收参数 MapListDate2个BeanJSON Spring MVC接收参数 -Map Spring MVC接收参数 -List Spring MVC接收参数 -dat ...
- Springboot接收参数
接收参数有三个方法. 1.接收id的方法: @RestController public class ControllerTest { //在这里读取配置文件 @Autowired private T ...
- 探究Spring Boot中的接收参数问题与客户端发送请求传递数据
结合此篇参考Spring框架学习笔记(9)--API接口设计相关知识及具体编码实现 在使用Spring Boot进行接收参数的时候,发现了许多问题,之前一直都很忙,最近才稍微有空研究一下此问题. 网上 ...
- Struts2学习---基本配置,action,动态方法调用,action接收参数
首先我们先来直接配置,然后再来讲原理: 第一步:jar包的引入: 我们可以到struts2的官网上下载: http://struts.apache.org/download.cgi#struts251 ...
随机推荐
- XML序列化、反序列化
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...
- pandas 设置单元格的值
import pandas as pd import pickle import numpy as np dates = pd.date_range() df = pd.DataFrame(np.ar ...
- python 正则表达式替换字符串中匹配的字符
import re street = '21 Ramkrishna Road' print(re.sub('Road$', 'Rd.', street)) 将结尾的Road用Rd.替换
- S4 对象系统
上一节中,我们介绍了 S3 系统.与大多数其他编程语言的面向对象系统不同,与那些类被定义为固定结构,且随着程序编译有确定的方法分派的系统相比,S3 系统显得非常不严谨.当我们定义一个 S3 类时,几乎 ...
- IIS中发布后出现Could not load file or assembly'System.Data.SQLite.dll' or one of its depedencies
[问题]在我本机的开发环境c#连接sqlite3没有问题,可是release版本移植到其他的机器就提示Could not load file or assembly'System.Data.SQLit ...
- Python处理HTML转义字符
抓网页数据经常遇到例如>或者 这种HTML转义符,抓到字符串里很是烦人. 比方说一个从网页中抓到的字符串: html = '<abc>' 用Python可以这样处理: import ...
- WebView 实现JS效果和a标签的点击事件
目前很多android app都可以显示web页面的界面,嵌入式开发,这个界面一般都是WebView这个控件加载出来的,学习该控件可以为你的app开发提升扩展性. 先说下WebView的一些优点: 可 ...
- HDU-4848 Wow! Such Conquering! (回溯+剪枝)
Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space is Super ...
- 在linux下出现cannot restore segment prot after reloc: Permission denied
应用程序连接oracle的库时会出现如下错误:XXXXX:: error while loading shared libraries: /usr/local/oracle/product/10.2. ...
- SecureCRT 7.2.0 Mac版密码无法保存的解决办法
参考:http://jingyan.baidu.com/article/915fc414fda5fb51394b20bd.html 我之前在网上找的解决办法都是SecureCRT 7.2.0 Mac ...