首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在java 10中使用lombok.Getter(lazy = true)时出现不兼容类型错误

在java 10中使用lombok.Getter(lazy = true)时出现不兼容类型错误
EN

Stack Overflow用户
提问于 2018-08-31 00:09:57
回答 1查看 765关注 0票数 2

我正在尝试使用reactorreactor.ipc.netty.http.client.HttpClient做一些缓存,并使用lombok的@Getter(lazy = true)将其初始化为惰性getter字段。

所有这些在Java8上都工作得很好,但在此代码片段中无法使用Java10的error: incompatible types: Duration cannot be converted to String进行编译

代码语言:javascript
运行
复制
@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));

但是它可以很好地编译

代码语言:javascript
运行
复制
@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));

如何知道哪里出了问题,以及如何解决问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-09 03:37:32

我建议将初始化代码移到一个单独的方法中。

代码语言:javascript
运行
复制
@Getter(lazy=true)
SomeType t = <complicatedInitializationCode>;

变成了

代码语言:javascript
运行
复制
@Getter(lazy=true)
SomeType t = initializeT();

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

披露:我是一名lombok开发者。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52101387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档