【问题标题】:YML file dosn't work with spring bootYAML 文件不适用于 Spring Boot
【发布时间】:2018-01-31 15:43:34
【问题描述】:

我有一个问题,我真的不知道如何解决它,对我来说这没有任何意义。谁能告诉我错误在哪里,因为我看不到。我正在使用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

【问题讨论】:

  • 你能正确地格式化你的 yml,它不可能读取它。 Yml 文件只有在格式正确的情况下才能工作。而且你在 yml 中没有spring.profiles.active。你只有spring.profiles

标签: java spring spring-boot web yaml


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2015-04-13
    • 2018-10-18
    • 2017-01-21
    • 1970-01-01
    • 2020-05-25
    • 2019-10-19
    • 2015-11-06
    • 2018-07-04
    • 2023-03-26
    相关资源
    最近更新 更多