我有一个java类,用于在读取.yml文件后设置对象。我遇到了无法在.yml文件中设置值的问题。下面是类的表示。映射是一个列表,同样有两个键值对。Offices是字符串列表,而assignment只是一个字符串。
public class System {
public Employee employee;
public List<Mapping> mappings;
public System(Employee emplyoee, List<Mapping> mappings) {
super();
this.employee = employee;
this.mappings = mappings;
}
public Employee getEmployee() {
return employee;
}
public void setSankalp(Employee sankalp) {
this.employee = sankalp;
}
public List<Mapping> getMappings() {
return mappings;
}
public void setMappings(List<Mapping> mappings) {
this.mappings = mappings;
}
public static class Employee {
public String name;
public String work;
public Employee(String name, String work) {
super();
this.name = name;
this.work = work;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
}
public static class Mapping {
public String assignment;
public List<String> offices;
public Mapping(String assignment, List<String> offices) {
super();
this.assignment= assignment;
this.offices= offices;
}
public String getAssignment() {
return assignment;
}
public void setAssignment(String assignment) {
this.assignment= assignment;
}
public List<String> getOffices() {
return offices;
}
public void setOffices(List<String> offices) {
this.offices= offices;
}
}
}
public class SystemWrapper {
public System system;
public SystemWrapper(System system) {
super();
this.system = system;
}
public System getSystem() {
return system;
}
public void setSystem(System system) {
this.system = system;
}
}.yaml的结构可以是什么?我正在尝试使用下面的.yaml,它根本不起作用。
system:
employee:
name: "andy"
work: "eng"
mappings:
- offices: ["new york", "dc"]
assignment: "full time"发布于 2020-01-15 14:20:10
尝试以下操作:
system:
employee:
name: "andy"
work: "eng"
mappings:
-mapping:
offices: ["new york", "dc"]
assignment: "full time"
-mapping:
offices: ["yorkshire", "dc"]
assignment: "part time"在您代码中,mappings是一个Mapping类的列表。
发布于 2020-01-15 16:24:51
您的yaml映射看起来没问题。我在示例Spring应用程序中尝试了它,它起作用了。我必须改变的是从类中删除构造器,并保留默认值。所以试着去掉这个:
public System(Employee emplyoee, List<Mapping> mappings) {
...
public Mapping(String assignment, List<String> offices) {
...
public Employee(String name, String work) {
...
public SystemWrapper(System system) {此外,您还在setEmployee中拼写错误
public Employee getEmployee() {
return employee;
}
public void setSankalp(Employee sankalp) {
this.employee = sankalp;
}此外,您还可以阅读有关解析in this blog post的更多信息,并测试不同的方法。
https://stackoverflow.com/questions/59745757
复制相似问题