SpringBoot配置自定义日期参数转换器
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.测试方式和效果

SpringBoot配置自定义日期参数转换器的更多相关文章
- SpringBoot自定义请求参数转换器
需求 我们可能对接客户的系统的时候,虽然Spring为我们提供的很多方便的转换器,但是遇到还是可能遇到需要自定义请求参数转换器的情况. 日期转换器 SpringBoot默认是没有配置日期转换器的我们可 ...
- SSM-SpringMVC-28:SpringMVC类型转换之自定义日期类型转换器
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 例子很简易,要明白的是思路,话不多说,开讲 上篇博客不是说springmvc默认的日期转换格式是yyyy/M ...
- Spring MVC__自定义日期类型转换器
WEB层采用Spring MVC框架,将查询到的数据传递给APP端或客户端,这没啥,但是坑的是实体类中有日期类型的属性,但是你必须提前格式化好之后返回给它们.说真的,以前真没这样做过,之前都是一口气查 ...
- spring boot 配置全局日期类型转换器
1. 首先自定义一个类型转换器 import org.springframework.core.convert.converter.Converter; import org.springframew ...
- struts2自定义日期类型转换器
在java web表单中提交的数据难免会有日期类型,struts2支持的日期类型是yyyy-MM-dd,如果是其他格式,就需要自己进行转换.比如yy-MM-dd 要完成自己定义的转换需要完成. 主要的 ...
- SpringBoot配置自定义美化Swagger2
1.添加maven依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springf ...
- struts_自定义日期类型转换器
1.问题:struts默认的日期类型是 xxxx-mm-dd,不能接收xxxx/mm//dd类型的日期 2.解决方案(继承DefaultTypeConverter,覆盖convertValue(Obj ...
- 在Spring MVC 中配置自定义的类型转换器
方法一: 实现spring mvc 自带的 Formatter 接口 1.创建一个类来实现Formatter接口 import org.springframework.format.Formatter ...
- struts2中配置全局日期类型转换器
1.编写一个类,继承StrutsTypeConverter,实现其中的convertFromString和convertToString方法,该类如下: package me.edu.utils; i ...
随机推荐
- flutter layout-child
一.Container 二.Padding 三.Center 四.Align 五.FittedBox 六.AspectRatio 七.ConstrainedBox 八.Baseline 九.Fract ...
- C# sort System.InvalidOperationException: Failed to compare two elements in the ar
System.InvalidOperationException: Failed to compare two elements in the array. ---> System.NullRe ...
- CodeForces-1234C-Pipes-dfs
You are given a system of pipes. It consists of two rows, each row consists of nn pipes. The top lef ...
- spring boot 结合jsp简单示例
引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- SSM 整合 Shiro
1. 导包 <!-- spring --> <dependency> <groupId>org.springframework</groupId> &l ...
- gmock 简单笔记
std::shared_ptr<MockThreadRCInvester> spMockaAcc; HelperThreadRCInvester helperAcc; // spMockA ...
- sql (4) key and Join
新建表: "Persons" 表:Id_P LastName FirstName Address City1 Adams John Oxford Street London2 Bu ...
- Git 如何使用ssh上传或者同步/下载项目到github
上传本地代码及更新代码到GitHub教程 上传本地代码 第一步:去github上创建自己的Repository,创建页面如下图所示: 红框为新建的仓库的https地址 第二步: echo " ...
- Python自学:第四章 在for循环中执行更多操作(2)
# -*- coding: GBK -*- magicians = ['alice', 'david', 'carolina'] for magician in magicians: print(ma ...
- zabbix_agentd 报错
1 cannot open PID file [/tmp/zabbix_agentd.pid]: [13] Permission denied 权限拒绝 很直观就是权限的问题 我的问题是这样的,这个p ...