如何在 snakeyaml 中用破折号解析字段名称?

How to parse field name with dash in snakeyaml?

我有 yaml 文件的片段:

field-name: my/data

但我无法使用方法名称创建 pojo setField-name

有没有办法解析这样的yaml文件?

您可以通过自定义 PropertyUtils 来处理此类情况

Constructor c = new Constructor(MyClass.class);
c.setPropertyUtils(new PropertyUtils() {
    @Override
    public Property getProperty(Class<? extends Object> type, String name) throws IntrospectionException {
      if ( name.indexOf('-') > -1 ) {
        name = toCameCase(name);
      }
      return super.getProperty(type, name);
    }
  });
Yaml yaml = new Yaml(c);
MyClass obj = (MyClass) yaml.load(input);