问题描述
我正在尝试在Spring Boot 1.5.1项目中设置并使用YAML作为配置文件。
I'm trying to setup and use a YAML as config file in my Spring Boot 1.5.1 project.
我的YAML文件如下所示:
My YAML file looks like the following:
hue:
user: cdKjsOQIRY8hqweAasdmx-WMsn
ip: "http://192.168.1.69"
scenes:
sunstatus:
enabled: true
id: 93yv8JekmAneCU9
group: 1
disable:
enabled: true
id: 93yv8JekmAneCU9
group: 6
读取hue.getUser()效果很好。但是,由于某些原因,hue.getScenes()返回null。我的Hue Config Java代码如下所示:
It works perfectly fine to read hue.getUser(). However, hue.getScenes() returns null for some reason. My Java code for the Hue Config looks like the following:
@Configuration
@ConfigurationProperties(prefix = "hue")
public class Hue {
private String user;
private String ip;
private Scenes scenes;
/*
* Getters and setters of course
*/
public class Scenes {
private Sunstatus sunstatus;
private Disable disable;
/*
* Getters and setters
*/
public class Sunstatus {
private boolean enabled;
private String id;
private String group;
/*
* Getters and setters
*/
}
public class Disable {
private boolean enabled;
private String id;
private String group;
/*
* Getters and setters
*/
}
}
}
我也尝试用hue.scenes.sunstatus,scenes.sunstatus和just的格式为每个类添加前缀。
I've tried as well to annotate the each class with prefix as well, both in the format of hue.scenes.sunstatus, scenes.sunstatus and just sunstatus as well.
另外,我也尝试使用@Value注释,但没有任何运气。
Additionally I also tried to use the @Value annotation a bit without any luck.
如果我将数据保留在application.yml或外部文件中,则结果相同。始终只能访问getUser()。
It's the same results if I keep the data in application.yml or in an external file. Can always only reach getUser().
我在这里做什么错了?
推荐答案
尝试一下。
@Configuration
@ConfigurationProperties(prefix = "hue")
public class Hue {
private String user;
private String ip;
private Scenes scenes = new Scenes();
/*
* Getters and setters of course
*/
public class Scenes {
private Sunstatus sunstatus = new Sunstatus();
private Disable disable = new Disable();
/*
* Getters and setters
*/
public class Sunstatus {
private boolean enabled;
private String id;
private String group;
/*
* Getters and setters
*/
}
public class Disable {
private boolean enabled;
private String id;
private String group;
/*
* Getters and setters
*/
}
}
}
这篇关于Spring Boot YAML Config无法读取所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!