簡體   English   中英

使用 lombok.Getter(lazy = true) 和 java 10 的不兼容類型錯誤

[英]Incompatible types error using lombok.Getter(lazy = true) with java 10

我試圖做一些使用緩存reactorreactor.ipc.netty.http.client.HttpClient並初始化它與龍目島的懶惰吸氣場@Getter(lazy = true)

在 Java 8 中一切正常,但無法編譯並error: incompatible types: Duration cannot be converted to String此代碼段上的 Java 10 error: incompatible types: Duration cannot be converted to String

@Value
public static class Translations {

    Map<String, Translation> translations;

    @Value
    public static class Translation {

        Map<String, String> content;

    }
}

@Getter(lazy = true)
Mono<Map<String, Translations.Translation>> translations = httpClient
        .get(String.format("%s/translations/%s", endpoint, translationGroup), Function.identity())
        .flatMap(it -> it.receive().aggregate().asByteArray())
        .map(byteArray -> {
            try {
                return objectMapper.readValue(byteArray, Translations.class);
            } catch (IOException e) {
                throw new UncheckedIOException("Failed to get translation for " + translationGroup, e);
            }
        })
        .map(Translations::getTranslations)
        .retryWhen(it -> it.delayElements(Duration.ofMillis(200)))
        .cache(Duration.ofMinutes(5))
        .timeout(Duration.ofSeconds(10));

但它編譯得很好

@Getter(lazy = true)
Mono<Map<String, Translations.Translation>> translations = Mono.just(new byte[]{})
        .map(byteArray -> {
            try {
                return objectMapper.readValue(byteArray, Translations.class);
            } catch (IOException e) {
                throw new UncheckedIOException("Failed to get translation for " + translationGroup, e);
            }
        })
        .map(Translations::getTranslations)
        .retryWhen(it -> it.delayElements(Duration.ofMillis(200)))
        .cache(Duration.ofMinutes(5))
        .timeout(Duration.ofSeconds(10));

如何知道出了什么問題以及如何解決?

我建議將初始化代碼移到單獨的方法中。

@Getter(lazy=true)
SomeType t = <complicatedInitializationCode>;

變成

@Getter(lazy=true)
SomeType t = initializeT();

private SomeType initializeT() {
    return <complicatedInitializationCode>;
}

披露:我是龍目島的開發人員。

暫無
暫無

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

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