Spring Boot 2.x 列表绑定问题 [英] Spring Boot 2.x list binding issues

查看:31
本文介绍了Spring Boot 2.x 列表绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到 Spring Boot 2x 后,我在绑定某些列表时遇到问题.该代码在 Spring 1.x 中工作,现在它在启动时引发绑定错误.这是我的 application.yml...

I'm having issues with binding some Lists after upgrading to Spring Boot 2x. The code worked in Spring 1.x and now it throws a binding error on startup. 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

这是我的组件类...

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

当我尝试跑步时,我得到...

When I try to run I get...

应用程序无法启动

说明:

无法将aws.geo-mappings"下的属性绑定到 java.util.List:

Failed to bind properties under 'aws.geo-mappings' to java.util.List:

Reason: Failed to bind properties under 'aws.geo-mappings' to java.util.List<com.example.demo.config.aws.GeoMappingEntry>

操作:

更新应用程序的配置

有没有其他人遇到过同样的问题?解决方案?建议?

Has anybody else run in to the same problem? Solutions? Suggestions?

推荐答案

以防万一其他人正在研究类似的问题.事实证明,一些在以前版本的 Spring 中不起作用的环境变量现在能够实际创建绑定,这就是错误的来源.

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.

在这种情况下,从环境变量到诸如 AWS_GEOMAPPINGS_0_REGIONS_0 = us-west-2 之类的列表的放松绑定以前不起作用,但现在起作用了.在 Spring Boot 2.0 之前,从环境变量设置它的唯一方法是通过 SPRING_APPLICATION_JSON 传递 JSON,这可行,但如果您尝试使用封装它的其他 JSON(如 Terraform).

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. 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.x 列表绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆