升级到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;
}
}
当我尝试跑步时,我得到......
申请失败
说明
无法绑定&#39; aws.geo-mappings&#39;到java.util.List:
Reason: Failed to bind properties under 'aws.geo-mappings' to java.util.List<com.example.demo.config.aws.GeoMappingEntry>
动作:
更新您的应用程序的配置
还有其他人遇到过同样的问题吗?解决方案?建议?
答案 0 :(得分:0)
以防万一其他人正在寻找类似的问题。事实证明,某些在Spring早期版本中不起作用的环境变量现在可以实际创建绑定,而这些正是来自错误的。
在这种情况下,以前从环境var轻松绑定到诸如AWS_GEOMAPPINGS_0_REGIONS_0 = us-west-2
之类的列表的操作以前没有用,但现在可以了。在Spring Boot 2.0之前,从环境var进行设置的唯一方法是通过SPRING_APPLICATION_JSON
传递JSON,该方法可以正常工作,但是如果您尝试使用封装了它的其他JSON(例如Terraform)进行部署,则会变得很复杂。