【发布时间】:2019-10-23 15:35:54
【问题描述】:
我正在尝试使用以下代码从 spring 的 application.properties 解析 LocalTime:
@Value("#{ T(java.time.LocalTime).parse('${app.myDateTime}')}")
private LocalTime myDateTime;
在 application.properties 我定义了这样的属性:
app.myDateTime=21:45:00
错误信息:
Failed to bind properties under 'app.my-date-time' to java.time.LocalTime:
Property: app.my-date-time
Value: 21:45:00
Origin: class path resource [application.properties]:44:15
Reason: failed to convert java.lang.String to @org.springframework.beans.factory.annotation.Value java.time.LocalTime
知道我做错了什么吗?谢谢。
调试模式出错:
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalTime] for value '21:45:00'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [21:45:00]
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
at org.springframework.boot.context.properties.bind.BindConverter$CompositeConversionService.convert(BindConverter.java:170)
at org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:96)
at org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:88)
at org.springframework.boot.context.properties.bind.Binder.bindProperty(Binder.java:313)
at org.springframework.boot.context.properties.bind.Binder.bindObject(Binder.java:258)
at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:214)
... 210 common frames omitted
Caused by: java.lang.IllegalArgumentException: Parse attempt failed for value [21:45:00]
at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:206)
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
... 217 common frames omitted
Caused by: java.time.format.DateTimeParseException: Text '21:45:00' could not be parsed at index 5
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalTime.parse(LocalTime.java:441)
at org.springframework.format.datetime.standard.TemporalAccessorParser.parse(TemporalAccessorParser.java:72)
at org.springframework.format.datetime.standard.TemporalAccessorParser.parse(TemporalAccessorParser.java:46)
at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:200)
... 218 common frames omitted
【问题讨论】:
-
您需要使用堆栈跟踪添加完整的错误消息
-
@Deadpool 不幸的是。应用程序未能以我所写的原因开始。 ://
-
启用调试级别日志记录并重新运行应用程序,我也想知道这个
app.my-date-time属性 -
@Deadpool 我用堆栈跟踪更新了问题,请检查一下
-
试试我对我有用的答案
标签: java spring spring-boot java-time application.properties