【发布时间】:2018-11-18 20:52:54
【问题描述】:
升级到 Spring Boot 2x 后,我遇到了绑定某些列表的问题。该代码在 Spring 1.x 中工作,现在它在启动时引发绑定错误。这是我的 application.yml...
aws:
geo-mappings:
- name: USA
regions:
- us-west-2
- us-west-1
- us-east-1
- us-east-2
- name: California
regions:
- us-west-2
这是我的组件类...
package com.example.demo.config.aws;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* Created by goer on 4/18/17.
*/
@Component
@Scope("singleton")
@ConfigurationProperties(prefix="aws")
public class AWSConfigProvider {
private List<GeoMappingEntry> geoMappings = new ArrayList<>();
public List<GeoMappingEntry> getGeoMappings() {
return this.geoMappings;
}
}
这是嵌套对象...
package com.example.demo.config.aws;
import java.util.ArrayList;
import java.util.List;
public class GeoMappingEntry {
private String name;
private List<String> regions = new ArrayList<>();
public GeoMappingEntry(String name, List<String> regions) {
this.name = name;
this.regions = regions;
}
}
当我尝试跑步时,我得到...
应用程序启动失败
说明:
无法将“aws.geo-mappings”下的属性绑定到 java.util.List:
Reason: Failed to bind properties under 'aws.geo-mappings' to java.util.List<com.example.demo.config.aws.GeoMappingEntry>
行动:
更新应用程序的配置
还有其他人遇到过同样的问题吗?解决方案?有什么建议吗?
【问题讨论】:
-
我想说你的
name字段需要一个setter。已初始化的集合不需要 setter,但未初始化的字段可能需要一个。 -
可能是2.0.1中修复的错误
-
我希望。我使用最新版本 2.0.2 创建了一个简化项目,但它无法正常工作。我怀疑新的绑定实现对某些东西有更严格的限制并造成了问题。
标签: java spring spring-boot yaml