需求:提交一个字符串到后端的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. 066、Weave如何与外网通信?(2019-04-09 周二)

    参考https://www.cnblogs.com/CloudMan6/p/7500550.html   Weave是一个私有的vxlan网络,默认与外部网络隔离.外部网络如何才能访问到weave中的 ...

  2. Js点击按钮下载文件到本地(兼容多浏览器)

    实现点击 用纯 js(非jquery)  下载文件到本地 自己尝试,加网上找了好久未果,如: window.open(url)   location.href=url   form表单提交   ifr ...

  3. java8 新特性 Optional容器类

    public class Godness { private String name; public Godness() { } public Godness(String name) { this. ...

  4. springboot08-jpa-mysql

    1.主要pom依赖: <!--jpa--> <dependency> <groupId>org.springframework.boot</groupId&g ...

  5. pycharm活动模板

    1.复制模板内容 2.文件设置: 点击+号:

  6. 谈谈 git 撤销操作

    在工作中,往往会因为一些误操作,想撤销代码,下面我简单介绍一下如何操作(如果不了解 git 的基础用法,可以点击这里:链接) 撤销本地修改 git checkout 文件 撤销所有文件 git che ...

  7. Codeforces712E

    传送门 here 题意: 有n个赌场,第i个赌场的胜率为$ P_i$,在第i个赌场若取胜则到达第$ i+1$个赌场,反之到达第$ i-1$个赌场 定义统治赌场$ L...R$为从赌场$ L$开始,从赌 ...

  8. ES6走一波 Proxy/Reflect

    Proxy:像拦截器,对目标对象修改等进行拦截,是一种元编程(meta programming),即修改JS语言本身. //生成proxy实例,两个参数都是对象,targetObj是要拦截的目标对象, ...

  9. 【blog】用emoji-java解决Emoji存储MySQL乱码问题

    emoji-java 核心方法: EmojiParser.parseToAliases(string); 将表情符号转为字符 EmojiParser.parseToUnicode(string); 将 ...

  10. 【tmos】SpringBoot登录拦截

    我的代码 public class WebLoginInterceptor implements HandlerInterceptor { @Autowired private WeixinConfi ...