如何使用 Spring Boot 从 YAML 属性将日期值绑定到 Java 映射键中?
YAML 属性文件:
settings:
calendar:
2018-06-04: 2018-06-25
2018-07-15: 2018-07-20
Run Code Online (Sandbox Code Playgroud)
属性类:
calendar地图的目的是将一个日期转换为另一个日期。
@ConfigurationProperties(prefix = "settings")
public class CalendarSettings {
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Map<LocalDate, LocalDate> calendar = new HashMap<>();
public Map<LocalDate, LocalDate> getCalendar() {
return calendar;
}
public void setCalendar(
Map<LocalDate, LocalDate> calendar) {
this.calendar = calendar;
}
}
Run Code Online (Sandbox Code Playgroud)
通过此设置,我收到以下异常:
Property: settings.null
Value: 2018-06-04
Reason: Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'null';
nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.time.LocalDate':
no matching editors or conversion strategy found
Run Code Online (Sandbox Code Playgroud)
CalendarSettings.class如果可能的话,我正在为如何注释日历属性而苦苦挣扎。或者如何创建某种反序列化器。
如果您在调试模式下运行应用程序,您可能会看到抛出以下异常:
Caused by: java.time.format.DateTimeParseException: Text '2018-06-04' could not be parsed at index 4
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) ~[na:1.8.0_162]
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) ~[na:1.8.0_162]
at java.time.LocalDate.parse(LocalDate.java:400) ~[na:1.8.0_162]
at org.springframework.format.datetime.standard.TemporalAccessorParser.parse(TemporalAccessorParser.java:69) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.format.datetime.standard.TemporalAccessorParser.parse(TemporalAccessorParser.java:46) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:200) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
Run Code Online (Sandbox Code Playgroud)
失败的原因是默认解析器 ( TemporalAccessorParser) 使用本地化日期转换器将 映射String到LocalDate. 要解决此问题,您可以编写自己的转换器:
Caused by: java.time.format.DateTimeParseException: Text '2018-06-04' could not be parsed at index 4
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) ~[na:1.8.0_162]
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) ~[na:1.8.0_162]
at java.time.LocalDate.parse(LocalDate.java:400) ~[na:1.8.0_162]
at org.springframework.format.datetime.standard.TemporalAccessorParser.parse(TemporalAccessorParser.java:69) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.format.datetime.standard.TemporalAccessorParser.parse(TemporalAccessorParser.java:46) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:200) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
Run Code Online (Sandbox Code Playgroud)
如果你用@ConfigurationPropertiesBinding注解注册这个组件,它会在解析应用程序属性时被拾取。该LocalDate.parse(CharSequence)方法使用ISO_LOCAL_DATE转换器,在您的情况下应该可以正常工作。
| 归档时间: |
|
| 查看次数: |
5402 次 |
| 最近记录: |