如何使用带有记录的配置处理器

问题描述 投票:0回答:2

我正在使用 spring-boot-configuration-processor 生成 spring-configuration-metadata.json 文件。

当我使用类进行配置时,描述取自字段上方的 javadoc,就像本例中使用 Lombok 一样:

@Getter
@ConfigurationProperties("my.second.properties")
public class MySecondProperties {

    /**
     * This is the description for stringProperty
     */
    private final String stringProperty;

    /**
     * This is the description for booleanProperty
     */
    private final boolean booleanProperty;

    public MySecondProperties(@DefaultValue("default value for stringProperty") String stringProperty,
                              boolean booleanProperty) {
        this.stringProperty = stringProperty;
        this.booleanProperty = booleanProperty;
    }
}

但是,当使用记录时,我无法获得描述。我已经创建了一条记录,如下例所示,但生成的 json 文件中的描述仍为空。

/**
 * @param stringProperty This is the description for stringProperty
 * @param booleanProperty This is the description for booleanProperty
 */
@ConfigurationProperties("my.fourth.properties")
public record MyFourthProperties (@DefaultValue("default value for stringProperty") String stringProperty,
                                  boolean booleanProperty) {
}

使用记录时如何确保描述填写完整?

spring-boot spring-boot-configuration
2个回答
2
投票

从 Spring Boot 3.2.0 开始,这似乎不可能。如果您希望

spring-boot-configuration-processor
生成属性级文档,则需要使用非记录 Java 类,可能带有 Lombok 注释。

spring-projects/spring-boot#29403更具体的注释:

...如果您将

@ConfigurationProperties
与记录类一起使用,则无法在配置元数据中自动生成描述。

GitHub 拉取请求尝试添加支持,但截至撰写本文时,它还不完整,并且不属于 Spring 库堆栈的一部分。


0
投票

尚未测试,但我会尝试这个选项:

按照添加附加元数据中的文档,您可以添加附加文件,其元数据将与原始文件合并。可以在那里添加缺少的描述。

此附加文件名为 META-INF/additional-spring-configuration-metadata.json.

当然,这个解决方案并不是最优的......

© www.soinside.com 2019 - 2024. All rights reserved.