你好,
从 Spring Boot 1.5.3.RELEASE 迁移到 2.1.3.RELEASE 后,我们遇到了属性绑定的错误(?)。
示例代码可以在这里找到。这是一个简单的应用程序,具有一个属性类,通过 @ConfigurationProperties 注释从 application.yml 接收值。
启动 DemoApplication 工作正常。
但是启动简单的 DemoApplicationTests 不起作用,因为演示使用“test”配置文件,因此属性类从“application-test.yml”接收其值。
如果“ignoreUnknownFields”不存在(即设置为 true),则不会发生异常。
Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'demo-properties' to com.example.demo.DemoApplication$DemoProperties
at org.springframework.boot.context.properties.bind.Binder.handleBindError(Binder.java:249) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:225) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:208) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:190) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.context.properties.ConfigurationPropertiesBinder.bind(ConfigurationPropertiesBinder.java:83) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.bind(ConfigurationPropertiesBindingPostProcessor.java:107) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
... 43 common frames omitted
Caused by: org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException: The elements [demo-properties.data[2]] were left unbound.
at org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler.checkNoUnboundElements(NoUnboundElementsBindHandler.java:88) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler.onFinish(NoUnboundElementsBindHandler.java:73) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.context.properties.bind.Binder.handleBindResult(Binder.java:235) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:222) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
... 47 common frames omitted
仅当 application-test.yml 中的列表小于 application.yml 中的列表时,才会出现该错误。
问候,曼努埃尔