在Java10中使用lombok.Getter(lazy=true)时出现不兼容类型错误
我正在尝试使用在Java10中使用lombok.Getter(lazy=true)时出现不兼容类型错误,java,lombok,java-10,Java,Lombok,Java 10,我正在尝试使用reactor,reactor.ipc.netty.http.client.HttpClient进行一些缓存,并使用lombok的@getter(lazy=true)将其初始化为lazy getter字段 所有这些都可以在Java 8中正常工作,但无法使用进行编译错误:不兼容的类型:在此代码段上,无法使用Java 10将Duration转换为String @Value public static class Translations { Map<String, Tr
reactor
,reactor.ipc.netty.http.client.HttpClient
进行一些缓存,并使用lombok的@getter(lazy=true)
将其初始化为lazy getter字段
所有这些都可以在Java 8中正常工作,但无法使用进行编译错误:不兼容的类型:在此代码段上,无法使用Java 10将Duration转换为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));
@Value
公共静态类翻译{
地图翻译;
@价值观
公共静态类翻译{
地图内容;
}
}
@Getter(lazy=true)
Mono translations=httpClient
.get(String.format(“%s/translations/%s”,端点,translationGroup),Function.identity()
.flatMap(it->it.receive().aggregate().asByteArray())
.map(byteArray->{
试一试{
返回objectMapper.readValue(byteArray,Translations.class);
}捕获(IOE异常){
抛出新的未选中异常(“未能获取“+translationGroup,e”的翻译);
}
})
.map(翻译::getTranslations)
.retryWhen(it->it.delayElements(持续时间(200)))
.cache(持续时间为5分钟)
.超时(持续时间为秒(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)
Mono translations=Mono.just(新字节[]{})
.map(byteArray->{
试一试{
返回objectMapper.readValue(byteArray,Translations.class);
}捕获(IOE异常){
抛出新的未选中异常(“未能获取“+translationGroup,e”的翻译);
}
})
.map(翻译::getTranslations)
.retryWhen(it->it.delayElements(持续时间(200)))
.cache(持续时间为5分钟)
.超时(持续时间为秒(10));
如何知道哪里出了问题以及如何解决问题?我建议将初始化代码移到一个单独的方法中
@Getter(lazy=true)
SomeType t = <complicatedInitializationCode>;
@Getter(lazy=true)
SomeType t=;
变成
@Getter(lazy=true)
SomeType t = initializeT();
private SomeType initializeT() {
return <complicatedInitializationCode>;
}
@Getter(lazy=true)
SomeType t=初始化集();
私有SomeType初始值设定项(){
返回;
}
披露:我是lombok开发者。你能分享一下
翻译的导入或定义吗?翻译也可以?帮助创建一个MCVE@nullpointer使用定义更新了示例。(请原谅我在半睡眠状态下键入)您的两个代码块都可以用Java11编译,您能提供完整的MCVE吗?