Spring Boot 使用 ConfigurationProperties 将 MonthDay 映射到 Map

问题描述 投票:0回答:1

假设 Spring 有一个

@ConfigurationProperties
课程。

@ConfigurationProperties
public class ConfigProps {

    Map<Month, MonthDay> daysMap = new EnumMap<>(Month.class);

}

在我的

application.yml
文件中,我希望类似的东西能够工作(映射到上面的字段)。

daysMap:
  JANUARY: 02-JAN
  MARCH: 15-DEC
  ...

不幸的是,我在映射时收到异常:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'daysMap.january' to java.time.MonthDay:

    Property: daysMap.january
    Value: "02-JAN"
    Origin: class path resource [application.yml]
    Reason: failed to convert java.lang.String to java.time.MonthDay (caused by java.time.format.DateTimeParseException: Text '02-JAN' could not be parsed at index 0)

Action:

Update your application's configuration

如何正确映射它?

java spring spring-boot
1个回答
0
投票

MonthDay 有特定的格式

--MM-dd
来指定月份和日期

ISO-8601 日历系统中的月日,例如--12-03。

因此请按照

application.yml

中的格式提供月份和日期
daysMap:
  JANUARY: --01-02
  MARCH: --12-15
© www.soinside.com 2019 - 2024. All rights reserved.