Springboot YAMLファイルデータとMapの相互転送
2011 ワード
現在のプロジェクトでは、k 8 sのpod、deployment、serviceなどのコンポーネントの作成によってyamlファイルの作成が実行されるなど、yamlファイルがプロファイルとして一般的です.
くだらないことは言わないで先にコードをつけなさい.
くだらないことは言わないで先にコードをつけなさい.
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.test.context.junit4.SpringRunner;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.*;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Test
public void contextLoads() {
Yaml yaml = new Yaml();
Resource resource = new ClassPathResource("b.yaml"); // resources yaml
Object result = null;
InputStream in = null;
try {
// File file = resource.getFile();
File dumpfile = new File("/Users/wyg/Desktop/b.yaml");
in = new FileInputStream(dumpfile);
result = yaml.load(in);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (in != null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Map resmap = (Map) result;
System.out.println(JSONObject.toJSONString(resmap));
// yaml
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml wyaml = new Yaml(dumperOptions);
File dumpfile = new File("/Users/wyg/Desktop/b.yaml");
try(FileWriter writer = new FileWriter(dumpfile)) {
wyaml.dump(resmap, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}