需求:提交一个字符串到后端的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:扩展类型转换器的更多相关文章

  1. [No000012C]WPF(4/7)类型转换器和标记扩展[译]

    介绍 之前讨论了WPF的基础架构,然后逐步开始学习布局面板,转换,介绍了不同的控件,容器,UI转换等.在这篇文章中,我将讨论每个创建XAML应用前的开发人员应该了解的关于XAML最重要的东西. 标记扩 ...

  2. wpf中xaml的类型转换器与标记扩展

    原文:wpf中xaml的类型转换器与标记扩展 这篇来讲wpf控件属性的类型转换器 类型转换器 类型转换器在asp.net控件中已经有使用过了,由于wpf的界面是可以由xaml组成的,所以标签的便利也需 ...

  3. [原创]java WEB学习笔记67:Struts2 学习之路-- 类型转换概述, 类型转换错误修改,如何自定义类型转换器

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. struts2 笔记02 文件上传、文件下载、类型转换器、国际化的支持

    Struts2的上传 1. Struts2默认采用了apache commons-fileupload  2. Struts2支持三种类型的上传组件 3. 需要引入commons-fileupload ...

  5. Struts2类型转换器

    概述 A .从一个HTML 表单到一个 Action 对象,类型转换是从字符串到非字符串.    –HTTP没有"类型" 的概念.每一项表单输入只可能是一个字符串或一个字符串数组. ...

  6. 关于StrutsTypeConverter类型转换器

    <!-- 问题1: 如何覆盖默认的错误消息? 1). 在对应的 Action 类所在的包中新建 ActionClassName.properties 文件, ActionClassName 即为 ...

  7. springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器

    1.    Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...

  8. XAML实例教程系列 - 类型转换器(Type Converter)七

    XAML实例教程系列 - 类型转换器(Type Converter) 分类: Windows 8 Silverlight2012-06-25 13:40 961人阅读 评论(0) 收藏 举报 butt ...

  9. mybatis类型转换器 - 自定义全局转换enum

    在数据模型.接口参数等场景部分属性参数为一些常量值,比如性别:男.女.若是定义成int或String类型,于是类型本身的范围太宽,要求使用者需要了解底层的业务方可知如何传值,那整体来看增加沟通成本,对 ...

随机推荐

  1. thinkcmf 相关

    后台登录,暂停关掉登录验证码: /Volumes/macHD2/myCode/wwwroot/app/admin/controller/PublicController.php $captcha = ...

  2. 可由inetd启动的协议无关时间获取服务器程序

    #include <time.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> ...

  3. SQL Server2012远程访问第二个实列

    现在有一台A电脑和一台B电脑,A是公司的服务器,安装了两个数据库实例(Sql Server 2012 和Sql Server2008 R2),B电脑在家,安装了Sql Server数据库两台电脑不在一 ...

  4. 小试XML实体注入攻击

    基础知识 XML(Extensible Markup Language)被设计用来传输和存储数据.关于它的语法,本文不准备写太多,只简单介绍一下. XML基本知识 1 2 3 4 5 <?xml ...

  5. Chrome之控制台使用【转载】

    原文链接:https://segmentfault.com/a/1190000002511877 关键API: console.log(); console.info(); console.warn( ...

  6. python笔记---数据基础类型

    s = 'laonanHai' s1 = s.capitalize() #首字母大写,其他字母小写 s2 = s.upper() #全部大写 s3 = s.lower() #全部小写 print(s, ...

  7. 「PKUWC 2018」Minimax

    传送门:Here 一道线段树合并好题 如果要维护点$ x$的信息,相当于合并$ x$的两棵子树 对于这题显然有:任何叶子节点的权值都可能出现在其祖先上 因而我们只需要在线段树合并的时候维护概率即可 我 ...

  8. Ettercap内网渗透

    最近网速很卡,于是想到有人在蹭网,怎么捉弄一下呢? 1.开启ettercap ettercap -G 2. Hosts->Scan,List 发现有几个新的地址192.168.1.71,192. ...

  9. Docker学习笔记1

    来源:第一本Docker书 基础命令 安装: apt-get install docker 启动: service docker start 创建并运行第一个容器: docker run -i -t ...

  10. CF1091F New Year and the Mallard Expedition

    题目地址:CF1091F New Year and the Mallard Expedition 题意比较复杂,整理一下: \(n\) 段,每段有两个属性:长度,地形(G,W,L) 有三种运动方式: ...