YML文件不适合弹簧启动

时间:2018-01-31 15:43:34

标签: java spring spring-boot web yaml

我有一个问题,我真的不知道如何解决它,而对我来说它并没有让人感觉到。任何人都可以告诉我哪里是错误,因为我看不到它。我正在使用spring-boot

这里我有yml文件:

spring:
   profiles: dev
 devices: 
  list: 
- id : TV1
  type : TV
  value : boolean
  name : TV_sufragerie
- id : TV2
  type : TV  
  value : boolean
  name : TV_dormitor
- id : Therme1
  type : Therme
  value : Double
  name : Therme-hol
- id : SmartBulb1
  type : SmartBulb
  value : SmartBulbValue
  name : SmartBulbDormitor

这就是我尝试从yml文件中获取数据的方式:

@Component
@ConfigurationProperties("devices")
public class DeviceYml {

private List<DevicesList> list = new ArrayList<>();

public static class DevicesList {

    private String id;

    private  String type ;

    private List<String> value = new ArrayList<>();

    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<String> getValue() {
        return value;
    }

    public void setValue(List<String> value) {
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "YAMLConfigDevice [id=" + id + ", type=" + type + ", value=" + value + ", name=" + name + "]";
    }

}
public List<DevicesList> getDevices() {
    return list;
}

public void setDevices(List<DevicesList> list) {
    this.list = list;
}

@Override
public String toString() {
    return "devicesList=" + list + "";
}
}

我现在在applicaton.properties中有这个:

spring.profiles.active=dev

1 个答案:

答案 0 :(得分:0)

首先要确保你的yml格式正确。 Yml依赖空间来正确解析。

假设yml格式正确,问题出在getter和setter上。他们的名字(调用getDevicessetDevices)期望yml包含一个名为devices的密钥,但您已将其称为devices.list