簡體   English   中英

如何以 ISO8601 格式將 Spring 引導解析屬性綁定到 LocalTime?

[英]How to have Spring Boot parse properties bound to LocalTime in ISO8601 format?

我有一個使用@ConfigurationProperties組件的 Spring Boot 2.2.6 應用程序,如下所示:

@Component
@ConfigurationProperties("myapplication")
public class MyApplicationSettings {
    private LocalTime startOfDay;

    // getter and setters here
}

我的集成測試突然開始在 Jenkins 上失敗,但在本地運行良好。 異常顯示如下:

Description:

Failed to bind properties under 'myapplication.start-of-day' to java.time.LocalTime:

    Property: myapplication.start-of-day
    Value: 06:00
    Origin: class path resource [application-integration-test.properties]:29:62
    Reason: failed to convert java.lang.String to java.time.LocalTime

Action:

Update your application's configuration

完整的異常跟蹤的根本原因是:

Caused by: java.time.format.DateTimeParseException: Text '06:00' could not be parsed at index 5
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
    at java.base/java.time.LocalTime.parse(LocalTime.java:463)
    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:217)

在對代碼進行了一些挖掘之后,我發現 Spring 將使用默認語言環境來解析LocalTime 由於我的本地機器使用 24 小時制,它可以工作。 構建服務器使用 12 小時語言環境,因此它無法解析字符串,因為沒有 am/pm 指示。

我的問題:

如何配置 Spring Boot 以將配置屬性解析為 ISO-8601 格式,獨立於 JVM 的默認區域設置?

我還檢查了文檔,但“屬性轉換”一章沒有提到LocalTime (或LocalDate

解決方案應包括以下步驟:

  1. 使用LocalTime字段(在您的情況下為 MyApplicationSettings)創建@ConfigurationProperties注釋的 class 並使用@EnableConfigurationProperties(MyApplicationSettings.class)@Configuration中注冊它
  2. application.properties中定義值:
myapplication.startOfDay=06:00:00
  1. 創建一個特殊的轉換器並確保它被 Spring 引導應用程序掃描和加載。 實際解析的邏輯當然可以不同,在那里實現你想要的任何東西,任何特定的格式,語言環境——隨便什么:
@Component
@ConfigurationPropertiesBinding
public class LocalTimeConverter implements Converter<String, LocalTime> {
    @Override
    public LocalTime convert(String source) {
        if(source==null){
            return null;
        }
        return LocalTime.parse(source, DateTimeFormatter.ofPattern("HH:mm:ss"));
    }
}
  1. 現在@ConfigurationProperties class 中的所有LocalTime條目都將通過此轉換器。

有辦法做到這一點,但不是直接的解決方案。 您需要創建一個自定義ConversionService bean,將此代碼片段放在應用程序文件中,它應該可以工作。

在這里,我注冊了一個新的轉換器,它將始終轉換為LocalTime格式的 LocalTime。 您也可以選擇使用其他日期格式化程序。

  @Bean
  public ConversionService conversionService() {
    ApplicationConversionService conversionService = new ApplicationConversionService();
    conversionService.addConverter(
        String.class,
        LocalTime.class,
        (Converter) source -> LocalTime.parse((String) source, DateTimeFormatter.ISO_LOCAL_TIME));
    return conversionService;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM