如何从 application.yml 文件中映射时间属性?

huangapple go评论88阅读模式
英文:

How to map time properties from application.yml?

问题

application.yml文件中,我遇到了映射时间的问题。

bc:
  aop:
    core:
      boot:
        business:
          dailyStartHour: 08:00
          dailyEndHour: 17:00
          lunchStartHour: 12:00
          lunchEndHour: 13:00

这是我的Java类:

@Data
@Configuration
@ConfigurationProperties(prefix = "bc.aop.core.boot.business")
public class BusinessHourProperties {

    private String dailyStartHour;
    private String dailyEndHour;
    private String lunchStartHour;
    private String lunchEndHour;

}

但是,当我设置应用程序时,抛出了一个解析错误:

Caused by: java.time.format.DateTimeParseException: Text '1020' could not be parsed at index 2
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) ~[na:1.8.0_231]
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) ~[na:1.8.0_231]
    at java.time.LocalTime.parse(LocalTime.java:441) ~[na:1.8.0_231]
    at java.time.LocalTime.parse(LocalTime.java:426) ~[na:1.8.0_231]
    at pnc.aop.core.ofac.boot.BusinessDiscussionDate.discussion(BusinessDiscussionDate.java:26) ~[classes/:na]

问题在于当我不将 BusinessHourProperties 用作配置时:

private String dailyStartHour = "08:00";
private String dailyEndHour = "17:00";
private String lunchStartHour = "12:00";
private String lunchEndHour = "13:00";

我就没有问题。发生了什么?

英文:

I have problem mapping the time added on application.yml

bc:
  aop:
    core:
      boot:
        business:
          dailyStartHour: 08:00
          dailyEndHour: 17:00
          lunchStartHour: 12:00
          lunchEndHour: 13:00

this is muy java class

@Data
@Configuration
@ConfigurationProperties(prefix = "bc.aop.core.boot.business")
public class BusinessHourProperties {


    private String dailyStartHour;
    private String dailyEndHour;
    private String lunchStartHour;
    private String lunchEndHour;


}

but when I setup the application thrown a parse error

Caused by: java.time.format.DateTimeParseException: Text '1020' could not be parsed at index 2
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) ~[na:1.8.0_231]
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) ~[na:1.8.0_231]
at java.time.LocalTime.parse(LocalTime.java:441) ~[na:1.8.0_231]
at java.time.LocalTime.parse(LocalTime.java:426) ~[na:1.8.0_231]
at pnc.aop.core.ofac.boot.BusinessDiscussionDate.discussion(BusinessDiscussionDate.java:26) ~[classes/:na]

The think is when I don't use BusinessHourProperties as a configuration like:

private String dailyStartHour = "08:00";
private String dailyEndHour = "17:00";
private String lunchStartHour = "12:00";
private String lunchEndHour = "13:00";

I don't have problem.

What's happening?

答案1

得分: 2

: 在你的 YAML 值中引起了这个问题,因为 YAML 使用 : 用于键值映射。所以按照这里的示例更改你的 YAML 属性。

bc:
  aop:
    core:
      boot:
        business:
          dailyStartHour: "08:00"
          dailyEndHour: "17:00"
          lunchStartHour: "12:00"
          lunchEndHour: "13:00"
英文:

: in your yaml value is causing this issue as yaml uses : for the key value mapping. So change your properties in the yaml as shown here.

bc:
  aop:
    core:
      boot:
        business:
          dailyStartHour: "08:00"
          dailyEndHour: "17:00"
          lunchStartHour: "12:00"
          lunchEndHour: "13:00"

huangapple
  • 本文由 发表于 2020年3月15日 12:18:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/60689700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定