Java Mysql Time类型 接收显示问题
最近写项目需要用的mysql中的Time时间类型
问题产生原因
想要实现在mysql中只存入 Time类型,如10:30
。这样可以方便存取,在后台接收和显示都比较方便。
产生的问题
但是这样问题就来了,Time类型传入的时候会用String类型传入,就会出现类型不匹配。
报错如下:
Field error in object 'danPlace' on field 'businessTime': rejected value [10:00]; codes [typeMismatch.danPlace.businessTime,typeMismatch.businessTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [danPlace.businessTime,businessTime]; arguments []; default message [businessTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Time' for property 'businessTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat @org.springframework.format.annotation.DateTimeFormat @com.ruoyi.common.annotation.Excel java.sql.Time] for value '10:00'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.Date] to type [@com.fasterxml.jackson.annotation.JsonFormat @org.springframework.format.annotation.DateTimeFormat @com.ruoyi.common.annotation.Excel java.sql.Time]]
解决方案
在实体类中添加注解 将显示时间和接收时间都进行格式化
@JsonFormat(pattern = "HH:mm") //显示时间类型 @DateTimeFormat(pattern="HH:mm") //接收时间类型
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗