我正在尝试使用reactor
,reactor.ipc.netty.http.client.HttpClient
进行一些缓存,并使用lombok的@Getter(lazy = true)
将其初始化为惰性getter字段。
在此代码段中,所有操作都可以在Java 8上正常运行,但无法在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));
如何知道出了什么问题以及如何解决?
答案 0 :(得分:1)
我建议将初始化代码移至单独的方法。
@Getter(lazy=true)
SomeType t = <complicatedInitializationCode>;
成为
@Getter(lazy=true)
SomeType t = initializeT();
private SomeType initializeT() {
return <complicatedInitializationCode>;
}
披露:我是lombok开发人员。