To use Maven: org.springframework:spring-beans:4.3.2.RELEASE2
public class YamlUtils {
private static final Logger logger = LogManager.getLogger(YamlUtils.class);
public static Map<String, Object> yaml2Map(String yamlSource) {
try {
YamlMapFactoryBean yaml = new YamlMapFactoryBean();
yaml.setResources(new ClassPathResource(yamlSource));
return yaml.getObject();
} catch (Exception e) {
logger.error("Cannot read yaml", e);
return null;
}
}
public static Properties yaml2Properties(String yamlSource) {
try {
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource(yamlSource));
return yaml.getObject();
} catch (Exception e) {
logger.error("Cannot read yaml", e);
return null;
}
}
}
public class TestYamlUtils {
private static final Logger logger = LogManager.getLogger(TestYamlUtils.class);
@Test
public void testYaml2Map() {
Map<String, Object> map = YamlUtils.yaml2Map("neiwai.yml");
Assert.assertNotNull(map);
map.forEach((k, v) -> {
logger.info("k={},v={}", k, v);
});
}
@Test
public void testYaml2Properties() {
Properties prop = YamlUtils.yaml2Properties("neiwai.yml");
Assert.assertNotNull(prop);
prop.forEach((k, v) -> {
logger.info("k={},v={}", k, v);
});
}
}