我正在尝试使用 Java 自动化一个流程,在该流程中我将 YAML 配置写入文件。 虽然我能够实现其中的大部分,但我仍然坚持最后一部分“链接”,并且需要帮助来通过识别适当的数据结构来解决这个问题。我也想了解我哪里出了问题。
预期的 YAML 是:
name: srlceos01
topology:
nodes:
srl:
kind: nokia_srlinux
image: ghcr.io/nokia/srlinux
ceos:
kind: ceos
image: ceos:4.25.0F
links:
- endpoints: ["srl:e1-1", "ceos:eth1"]
我的理解是“链接”是“端点”数组列表的数组。我得到以下结果:
实际结果:
!!xx.xx.xx.bean.Container (Unwanted and wanted to remove)
links:
linkArray:
- endpoints:
- endpoints:
- srl:e1-1
- ceos:eth1
- null
- null
- null
- null
name: srlceos01
topology:
srl:
image: ghcr.io/nokia/srlinux
kind: nokia_srlinux
我的代码:
public class Container {
private String name;
private Map<String, Nodes> topology;
private LinksArray links;
}
public class LinksArray {
private Links[] linkArray;
}
public class Links {
private List<Endpoint> endpoints;
}
public class Endpoint {
private String[] endpoints;
}
public class Nodes {
private String kind;
private String image;
}
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Tag;
import xx.xx.Container;
@Service
public class GenerateYamlFileImpl {
public void writeYamlfile(Container yamlConfig) {
DumperOptions dpo = new DumperOptions();
dpo.setDefaultFlowStyle(FlowStyle.BLOCK);
Yaml yaml = new Yaml(dpo);
System.out.println(yaml.dump(yamlConfig));
}
}
@SpringBootApplication
@ComponentScan("xx.xx.xx")
public class EmulateGuiBackendApplication {
public static void main(String[] args) {
SpringApplication.run(EmulateGuiBackendApplication.class, args);
GenerateYamlFile gf = new GenerateYamlFile();
gf.createYamlFile();
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xx.xx.Container;
import xx.xx.Endpoint;
import xx.xx.Links;
import xx.xx.LinksArray;
import xx.xx.Nodes;
import xx.xx.service.GenerateYamlFileImpl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@Component
public class GenerateYamlFile {
Logger logger = LogManager.getLogger(GenerateYamlFile.class);
public void createYamlFile() {
Container container = new Container();
container.setName("srlceos01");
Map<String, Nodes> topo = new LinkedHashMap();
Nodes node = new Nodes();
node.setImage("ghcr.io/nokia/srlinux");
node.setKind("nokia_srlinux");
topo.put("srl", node);
String[] link = {"srl:e1-1","ceos:eth1"};
Endpoint end = new Endpoint();
end.setEndpoints(link);
List<Endpoint> endpointList = new ArrayList<>();
endpointList.add(end);
Links newLink = new Links();
newLink.setEndpoints(endpointList);
Links[] lkarray = new Links[5];
lkarray[0]=newLink;
LinksArray lArr = new LinksArray();
lArr.setLinkArray(lkarray);
container.setTopology(topo);
container.setLinks(lArr);
GenerateYamlFileImpl yamlService = new GenerateYamlFileImpl();
yamlService.writeYamlfile(container);
}
}
您的类层次结构未正确建模 YAML 结构。具体来说,你的
LinksArray
不是一个数组,而是一个 POJO,并且 Endpoint
是多余的。
你需要的是这个:
public class Container {
private String name;
private Map<String, Nodes> topology;
private List<Link> links;
}
public class Link {
private List<String> endpoints;
}
至于省略根标签,请执行以下操作:
DumperOptions dpo = new DumperOptions();
dpo.setDefaultFlowStyle(FlowStyle.BLOCK);
Representer representer = new Representer(dpo);
representer.addClassTag(Container.class, Tag.MAP);
Yaml yaml = new Yaml(representer);
我从原始代码中保留了
FlowStyle.BLOCK
,尽管这会阻止你获得["srl:e1-1", "ceos:eth1"]
——显然你会得到一个块式序列。我想这就是你想要的。