Spring boot 如何使ConfigurationProperties bean和@FeignClient接口中的验证注释一起工作?

Spring boot 如何使ConfigurationProperties bean和@FeignClient接口中的验证注释一起工作?,spring-boot,spring-cloud,bean-validation,spring-cloud-feign,configurationproperties,Spring Boot,Spring Cloud,Bean Validation,Spring Cloud Feign,Configurationproperties,假设我有这个application.yml(它将依赖于环境,例如通过Spring概要文件): 和匹配的Java样式配置属性类: @Configuration @ConfigurationProperties("app.remote") public class MyRemoteProperties { @NotBlank private String url; // matching getter/setter... } @Validated @Configurat

假设我有这个application.yml(它将依赖于环境,例如通过Spring概要文件):

和匹配的Java样式配置属性类:

@Configuration
@ConfigurationProperties("app.remote")
public class MyRemoteProperties {

    @NotBlank
    private String url;

    // matching getter/setter...
}
@Validated
@Configuration
@ConfigurationProperties("app.remote")
public class MyRemoteProperties {

    @NotBlank
    private String url;

    // matching getter/setter...
}
我想为我的远程url使用某种客户端:

@Service
@FeignClient(value = "remote", url = "${app.remote.url}")
public interface MyRemote {

    @GetMapping("/what/ever/rest/api")
    String stuff();

}
不幸的是,我无法获得
MyRemoteProperties
的验证工作,例如,当
app.remote.url
属性为空时,应用程序无法启动(Spring在连接
MyRemote
bean时失败),我得到以下错误:

原因:java.lang.IllegalStateException:没有针对的假客户端 定义了负载平衡。你忘了包括在内吗 春季云启动netflix ribbon

(我不希望进行负载平衡;我假设这是因为URL在某个点是空的,然后它希望在错误消息中出现一些负载平衡器配置)

或者我不知道如何将其插入MyRemote接口的配置中,例如,我也尝试过:

@FeignClient(value = "remote", configuration = MyRemoteProperties.class)
但同样的结果

我如何让这个验证工作正常进行

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.8.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

不要忘记Java属性类上的
@Validated
注释:

@Configuration
@ConfigurationProperties("app.remote")
public class MyRemoteProperties {

    @NotBlank
    private String url;

    // matching getter/setter...
}
@Validated
@Configuration
@ConfigurationProperties("app.remote")
public class MyRemoteProperties {

    @NotBlank
    private String url;

    // matching getter/setter...
}

您的应用程序无法启动是因为缺少属性,而不是因为您不需要一个未定义的负载平衡客户端(从而使其错误消息更加尴尬)。

事实上,我错过了properties类中的
@Validated
。Baeldung的文章中没有提到这一点:(我先读了…)而它在Spring Boot文档中://通过查看测试类
Spring Boot/blob/master/Spring Boot project/Spring Boot/src/test/java/org/springframework/Boot/context/properties/configurationpropertiests.java