springboot:扩展类型转换器
需求:提交一个字符串到后端的java.sql.Time类型,就报错了:
Failed to convert property value of type [java.lang.String] to required type [java.sql.Time]
正常提交到java.util.Date类型是没有问题的。
所以这里就需要扩展内置的springmvc的转换器
代码如下:
WebConfig : 添加新的类型转换器
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; import com.csget.web.converter.StringToTimeConverter; @Configuration
public class WebConfig { @Autowired
private RequestMappingHandlerAdapter requestMappingHandlerAdapter; @PostConstruct
public void addConversionConfig() {
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) requestMappingHandlerAdapter
.getWebBindingInitializer();
if (initializer.getConversionService() != null) {
GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService();
genericConversionService.addConverter(new StringToTimeConverter());
}
}
}
StringToTimeConverter :类型转换器的具体实现
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date; import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter; public class StringToTimeConverter implements Converter<String, Time> {
public Time convert(String value) {
Time time = null;
if (StringUtils.isNotBlank(value)) {
String strFormat = "HH:mm";
int intMatches = StringUtils.countMatches(value, ":");
if (intMatches == 2) {
strFormat = "HH:mm:ss";
}
SimpleDateFormat format = new SimpleDateFormat(strFormat);
Date date = null;
try {
date = format.parse(value);
} catch (Exception e) {
e.printStackTrace();
}
time = new Time(date.getTime());
}
return time;
} }
springboot:扩展类型转换器的更多相关文章
- [No000012C]WPF(4/7)类型转换器和标记扩展[译]
介绍 之前讨论了WPF的基础架构,然后逐步开始学习布局面板,转换,介绍了不同的控件,容器,UI转换等.在这篇文章中,我将讨论每个创建XAML应用前的开发人员应该了解的关于XAML最重要的东西. 标记扩 ...
- wpf中xaml的类型转换器与标记扩展
原文:wpf中xaml的类型转换器与标记扩展 这篇来讲wpf控件属性的类型转换器 类型转换器 类型转换器在asp.net控件中已经有使用过了,由于wpf的界面是可以由xaml组成的,所以标签的便利也需 ...
- [原创]java WEB学习笔记67:Struts2 学习之路-- 类型转换概述, 类型转换错误修改,如何自定义类型转换器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- struts2 笔记02 文件上传、文件下载、类型转换器、国际化的支持
Struts2的上传 1. Struts2默认采用了apache commons-fileupload 2. Struts2支持三种类型的上传组件 3. 需要引入commons-fileupload ...
- Struts2类型转换器
概述 A .从一个HTML 表单到一个 Action 对象,类型转换是从字符串到非字符串. –HTTP没有"类型" 的概念.每一项表单输入只可能是一个字符串或一个字符串数组. ...
- 关于StrutsTypeConverter类型转换器
<!-- 问题1: 如何覆盖默认的错误消息? 1). 在对应的 Action 类所在的包中新建 ActionClassName.properties 文件, ActionClassName 即为 ...
- springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器
1. Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...
- XAML实例教程系列 - 类型转换器(Type Converter)七
XAML实例教程系列 - 类型转换器(Type Converter) 分类: Windows 8 Silverlight2012-06-25 13:40 961人阅读 评论(0) 收藏 举报 butt ...
- mybatis类型转换器 - 自定义全局转换enum
在数据模型.接口参数等场景部分属性参数为一些常量值,比如性别:男.女.若是定义成int或String类型,于是类型本身的范围太宽,要求使用者需要了解底层的业务方可知如何传值,那整体来看增加沟通成本,对 ...
随机推荐
- java基础之反射---重要
java反射: 反射是框架设计的灵魂 (使用的前提条件:必须先得到代表的字节码的Class,Class类用于表示.class文件(字节码)): 1:获取Class字节码文件对象的三种方式: /** ...
- linux centos 7 nodejs 的安装
先SSH 进到终端 随便一目录,或者/temp下 下载nodejs: (如果下载不了,官网 https://nodejs.org/en/download/复制LINUX版本的链接过来) wget ht ...
- C#一例绘制字体不清晰的解决办法
public static Bitmap GetPieWithText(String text, Color color, Color fontColor,Font font) { ; Bitmap ...
- 10.tesseract
1.Tesseract-OCR简介 一个Google支持的开源的OCR图文识别开源项目.支持多种语言(我使用的是3.02 版本,支持包括英文,简体中文,繁体中文),支持Windows,Linux,M ...
- 九、uboot 代码流程分析---relloc_code
执行完 board_init_f 后,重新跳转回 _main 中执行. 9.1 relloc_code 前 9.1.1 gd 设置 在调用board_init_f()完成板卡与全局结构体变量 gd 的 ...
- win10编译caffe调用matlab接口
参考 https://www.cnblogs.com/njust-ycc/p/5776286.html https://www.cnblogs.com/heately/p/7922521.html
- oracle 利用over 查询数据和总条数,一条sql搞定
select count(*) over()总条数 ,a.*from table a
- [C++]四分树(Quadtrees)
[本博文非博主原创,思路与题目均摘自 刘汝佳<算法竞赛与入门经典(第2版)>] 四分树Quadtrees 一幅图有1024个点, 可以对图平均分成4块, 并且子图也可以再往下分, 直到一个 ...
- MIME 类型
关于读音 为了防止大家出去丢人,我先示范一下,MIME应该独坐[maim],听起来就好像“男人”的英语法人一样. 浏览器和MIME的关系 浏览器依靠MIME类型解释网页. 每当浏览器请求一个web页面 ...
- 【tmos】mvn package相关知识点(待补充...)
SpringBoot项目打包跳过测试 <build> <plugins> <plugin> <groupId>org.springframework.b ...