我正在尝试使用reactor
,reactor.ipc.netty.http.client.HttpClient
做一些缓存,并使用lombok的@Getter(lazy = true)
将其初始化为惰性getter字段。
所有这些在Java8上都工作得很好,但在此代码片段中无法使用Java10的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));
如何知道哪里出了问题,以及如何解决问题?
发布于 2018-10-09 03:37:32
我建议将初始化代码移到一个单独的方法中。
@Getter(lazy=true)
SomeType t = <complicatedInitializationCode>;
变成了
@Getter(lazy=true)
SomeType t = initializeT();
private SomeType initializeT() {
return <complicatedInitializationCode>;
}
披露:我是一名lombok开发者。
https://stackoverflow.com/questions/52101387
复制相似问题