使用SnakeYaml将java对象写入文件 - 使用Yaml.dump()时不会写出内部对象列表

时间:2016-11-06 20:35:48

标签: java spring-boot yaml snakeyaml

我在使用SnakeYaml将java对象写入yml文件时遇到问题。我已经设置了一个spring boot项目,其中包含一个简单的application.yml,我将其转换为一组java对象。在实际应用程序中添加了一些对象,当应用程序完成时,我想写出新的application.yml。

输入application.yml如下所示:

sample:
  name: toyota
  cars:
     - model: prius
       colour: silver
     - model: avensis
       colour: red

映射此application.yml:

的java对象
@Configuration
@ConfigurationProperties(prefix="sample")
public class Config {

    private String name;
    private final List<Car> cars = new ArrayList<>();

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Car> getCars() {
        return cars;
    }
}

写出yaml的示例类:

@Component
public class Example {

    @Autowired
    private Config config;

    @PostConstruct
    private void init(){
        Yaml yaml = new Yaml();
        StringWriter writer = new StringWriter();
        yaml.dump(config, writer);      
        System.out.println(writer.toString());          
    }
}

输出只是:name: toyota(省略了一些也被写出的yaml设置)。但是我希望输出与输入完全相同:

sample:
  name: toyota
  cars:
     - model: prius
       colour: silver
     - model: avensis
       colour: red

有办法做到这一点吗?

编辑:

用户flyx正确地指出了以下documentation

我调整后的代码:

Constructor constructor = new Constructor(Config.class);
TypeDescription configDescription = new TypeDescription(Config.class);
configDescription.putListPropertyType("cars", Car.class);
constructor.addTypeDescription(configDescription);
Yaml yaml = new Yaml(constructor);
StringWriter writer = new StringWriter();
yaml.dump(config, writer);      
System.out.println(writer.toString()); 

提供以下输出:

$$beanFactory: !!org.springframework.beans.factory.support.DefaultListableBeanFactory
  allowBeanDefinitionOverriding: true
  allowEagerClassLoading: true
  autowireCandidateResolver: !!org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver {}
  beanClassLoader: !!sun.misc.Launcher$AppClassLoader {}
  beanExpressionResolver: !!org.springframework.context.expression.StandardBeanExpressionResolver {}
  cacheBeanMetadata: true
  conversionService: null
  dependencyComparator: !!org.springframework.core.annotation.AnnotationAwareOrderComparator {}
  parentBeanFactory: null
  serializationId: application
  tempClassLoader: null
  typeConverter: !!org.springframework.beans.SimpleTypeConverter {conversionService: null}
name: toyota

2 个答案:

答案 0 :(得分:1)

由于类型擦除,SnakeYAML无法在运行时获取autoless less css的元素类型。文档中有example您需要做的事情。

这样的事情应该有效:

List

答案 1 :(得分:1)

如果没有Car列表中的显式设置器,则不会打印列表。

更改后:

private final List<Car> cars = new ArrayList<>();

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public List<Car> getCars() {
    return cars;
}

对此:

private List<Car> cars = new ArrayList<>();

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public void setCars(List<Car> cars) {
    this.cars = cars;
}
public List<Car> getCars() {
    return cars;
}

代码有效。