我想使用snakeyaml 1.24读取这样的yaml配置文件:

monitor: monitor 0
folders:
  - path/folder1
  - path/folder2
filters:
  includes:
    - first filter 0
    - second filter 0
  excludes:
    - first exclusion 0
    - second exclusion 0

includeSubdirs: true
processors:
  - processor: processor#0
    args:
      - arg0: value0
      - arg1: value1
  - processor: processor#1
    args:
      - arg2: value2

---

monitor: myMonitor 1
folders:
  - path/folder3
  - path/folder4
filters:
  includes:
    - first filter 1
  excludes:
    - first exclusion 1
    - second exclusion 1
    - third exclusion 1

includeSubdirs: true
processors:
  - processor: processor#2
    args:
      - arg3: value3
      - arg4: value4
      - arg5: value5
  - processor: processor#3
    args:
      - arg6: value6
      - arg7: value7

每个文档都被序列化为Monitor类实例,但我希望args被序列化为java.util.Map
这是我的主课:
public class WWatchdog {
    public static void main(String[] args) {
        Constructor constructor = new Constructor(MonitorSettings.class);
        Yaml settings = new Yaml(constructor);

        try {
            for (Object object : settings.loadAll(new FileReader("wwatchdog-core/monitors.yml"))) {
                if (object instanceof MonitorSettings) {
                    MonitorSettings monitorSettings = (MonitorSettings)object;
                    System.out.println(settings.dump(monitorSettings));
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

和波乔
public class MonitorSettings {
    private String monitor;
    private List<String> folders;
    private MonitorFilterSettings filters;
    private boolean includeSubdirs;
    private List<ProcessorSettings> processors;

// Getters and setters
}

public class MonitorFilterSettings {
    private List<String> includes;
    private List<String> excludes;

// Getters and setters
}

public class ProcessorSettings {
    private String processor;
    private Map<String, String> args;

// Getters and setters
}


我得到以下错误:
Exception in thread "main" Cannot create property=processors for JavaBean=org.nemert.wwatchdog.Monitor@6f2b958e
 in 'reader', line 1, column 1:
    monitor: myMonitor
    ^
Cannot create property=args for JavaBean=org.nemert.wwatchdog.ProcessorConfig@3d012ddd
 in 'reader', line 15, column 5:
      - processor: my processor
        ^
No suitable constructor with 2 arguments found for interface java.util.Map
 in 'reader', line 17, column 7:
          - arg0: value0
          ^

 in 'reader', line 15, column 3:
      - processor: my processor
      ^

    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:268)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:149)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:309)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:215)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:205)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:164)
    at org.yaml.snakeyaml.constructor.BaseConstructor.getData(BaseConstructor.java:129)
    at org.yaml.snakeyaml.Yaml$1.next(Yaml.java:548)
    at org.nemert.wwatchdog.WWatchdog.main(WWatchdog.java:17)
Caused by: Cannot create property=args for JavaBean=org.nemert.wwatchdog.ProcessorConfig@3d012ddd
 in 'reader', line 15, column 5:
      - processor: my processor
        ^
No suitable constructor with 2 arguments found for interface java.util.Map
 in 'reader', line 17, column 7:
          - arg0: value0
          ^

    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:268)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:149)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:215)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:205)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequenceStep2(BaseConstructor.java:376)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequence(BaseConstructor.java:360)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructSequence.construct(Constructor.java:516)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:215)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:205)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.newInstance(Constructor.java:283)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:245)
    ... 8 more
Caused by: org.yaml.snakeyaml.error.YAMLException: No suitable constructor with 2 arguments found for interface java.util.Map
    at org.yaml.snakeyaml.constructor.Constructor$ConstructSequence.construct(Constructor.java:582)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:215)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:205)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:245)
    ... 18 more

能做我想做的事吗?我对snakeyaml还不熟悉,我在谷歌上搜索了一个没有成功的解决方案。

最佳答案

我回答自己是因为我意识到这是个愚蠢的问题。
在标记arg<n>之前去掉连字符就足够了。也就是说,像- arg0: value0这样的线条变成了arg0: value0并且工作起来像一个符咒。
如果我浪费了某人的时间,请原谅。

10-08 06:46