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.测试方式和效果

最新文章

  1. centos上安装pygame
  2. Linux学习笔记
  3. 去除表单自动填充时,-webkit浏览器默认给文本框加的黄色背景
  4. My97DatePicker 与 某个CSS冲突 ,导致无法显示
  5. java: cannot execute binary file
  6. 可用的CSS文字两端对齐
  7. margin的auto的理解
  8. Java实现的电脑已连接WiFi热点的导入导出小工具
  9. golang-在gin中cookie跨域设置(配合ajax)
  10. js动态生成层方法 不懂得加QQ 2270312758
  11. LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java
  12. RHCSA考试真题
  13. Angular组件生命周期钩子
  14. Windows下解压分卷压缩方法
  15. CentOS7 重启网卡Failed to start LSB: Bring up/down networking.解决方法
  16. Java应用集群下的定时任务处理方案(mysql)
  17. 使用delphi 10.2 开发linux 上的webservice
  18. 在MNIST数据集,实现多个功能的tensorflow程序
  19. 1000! mod 10^250
  20. bzoj 3252: 攻略 -- 长链剖分+贪心

热门文章

  1. jquery scrollTop() 方法
  2. mybatis浅显认识
  3. startup_stm32f10x_xx.s 启动代码文件选择
  4. Activiti学习笔记目录
  5. JAVA-基础(Stream流)
  6. js 阻止事件
  7. css 苹方字体
  8. leetcode-241-为运算表达式设置优先级*
  9. [JZOJ4665] 【GDOI2017模拟7.21】数列
  10. 如何查看redis占用内存大小