我正在尝试使用 reactor
、reactor.ipc.netty.http.client.HttpClient
进行一些缓存,并使用 lombok 的 将其初始化为惰性 getter 字段>@Getter(惰性 = 真)
。
在 Java 8 中一切正常,但无法编译并出现 错误:不兼容的类型:持续时间无法转换为字符串
在此代码段中使用 Java 10
@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>;
}
披露:我是 lombok 开发者。
关于java - 在 java 10 中使用 lombok.Getter(lazy = true) 时出现不兼容类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52101387/