简体   繁体   English

Spring Boot 2.x列表绑定问题

[英]Spring Boot 2.x list binding issues

I'm having issues with binding some Lists after upgrading to Spring Boot 2x. 升级到Spring Boot 2x后,我在绑定某些列表时遇到问题。 The code worked in Spring 1.x and now it throws a binding error on startup. 该代码在Spring 1.x中有效,现在在启动时引发绑定错误。 Here's my 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

Here's my component class... 这是我的组件类...

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;
    }


}

Here's the nested object... 这是嵌套对象...

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;
    }
}

When I try to run I get... 当我尝试跑步时,我得到...


APPLICATION FAILED TO START 申请开始失败


Description: 描述:

Failed to bind properties under 'aws.geo-mappings' to java.util.List: 无法将“ 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>

Action: 行动:

Update your application's configuration 更新您的应用程序的配置

Has anybody else run in to the same problem? 还有其他人遇到过同样的问题吗? Solutions? 解决方案? Suggestions? 建议?

Just incase anybody else is looking at a similar issue. 以防万一其他人正在寻找类似的问题。 It turned out that some environment variables that didn't work in previous versions of Spring are now able to actually create bindings and that's were the errors were coming from. 事实证明,某些在Spring早期版本中不起作用的环境变量现在可以实际创建绑定,而这正是错误的来源。

In this case relaxed binding from an environment var to a list such as AWS_GEOMAPPINGS_0_REGIONS_0 = us-west-2 wasn't working before but now it does. 在这种情况下,从环境var轻松绑定到诸如AWS_GEOMAPPINGS_0_REGIONS_0 = us-west-2以前并不起作用,但现在可以了。 Before Spring Boot 2.0 the only way to set this from an environment var was to pass JSON via SPRING_APPLICATION_JSON , which works, but gets complicated if you are trying to deploy using some other JSON that encapsulates it (like Terraform). 在Spring Boot 2.0之前,从环境var进行设置的唯一方法是通过SPRING_APPLICATION_JSON传递JSON,该方法可以正常工作,但是如果您尝试使用封装了它的其他JSON(例如Terraform)进行部署,则会变得复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM