SpringBoot配置自定义日期参数转换器
2024-08-30 15:07:41
1.自定义参数转换器
自定义参数转换器必须实现Converter接口
/**
* Created by IntelliJ IDEA.
*
* @Auther: ShaoHsiung
* @Date: 2018/8/29 15:42
* @Title:
* @Description: 自定义日期转换器
*/
public class DateConverter implements Converter<String,Date> {
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public Date convert(String s) {
if ("".equals(s) || s == null) {
return null;
}
try {
return simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
2.配置转换器
自定义WebMvcConfig继承WebMvcConfigurerAdapter,在addFormatters方法中进行配置:
/**
* Created by IntelliJ IDEA.
*
* @Auther: ShaoHsiung
* @Date: 2018/8/29 15:41
* @Title:
* @Description:
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new DateConverter());
}
}
3.编写测试controller
/**
* Created by IntelliJ IDEA.
*
* @Auther: ShaoHsiung
* @Date: 2018/8/29 11:14
* @Title:
* @Description: 测试日期转换器
*/
@RestController
public class HelloController {
@RequestMapping(method = RequestMethod.GET, path = "/hello")
public String hello(Date date) {
// 测试简单类型
System.out.println(date); return "Hello SpringBoot";
}
}
4.测试方式和效果
最新文章
- centos上安装pygame
- Linux学习笔记
- 去除表单自动填充时,-webkit浏览器默认给文本框加的黄色背景
- My97DatePicker 与 某个CSS冲突 ,导致无法显示
- java: cannot execute binary file
- 可用的CSS文字两端对齐
- margin的auto的理解
- Java实现的电脑已连接WiFi热点的导入导出小工具
- golang-在gin中cookie跨域设置(配合ajax)
- js动态生成层方法 不懂得加QQ 2270312758
- LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java
- RHCSA考试真题
- Angular组件生命周期钩子
- Windows下解压分卷压缩方法
- CentOS7 重启网卡Failed to start LSB: Bring up/down networking.解决方法
- Java应用集群下的定时任务处理方案(mysql)
- 使用delphi 10.2 开发linux 上的webservice
- 在MNIST数据集,实现多个功能的tensorflow程序
- 1000! mod 10^250
- bzoj 3252: 攻略 -- 长链剖分+贪心