首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SnakeYaml“找不到属性错误”

SnakeYaml“找不到属性错误”
EN

Stack Overflow用户
提问于 2018-10-03 03:51:58
回答 1查看 7.8K关注 0票数 6

下面是我的config.yml的一部分:

代码语言:javascript
复制
#Authenctication
AuthenticationConfig:
  AuthencticationType: LDAP
  LDAPConfig:
     LDAPUrl: ldap://localhost:389
     ConnectionType: simple
     LDAPSecurityConfig:
        RootDN: cn=manager,dc=maxcrc,dc=com
        RootPassword: secret
        UserSearchDN: ou=People,dc=maxcrc,dc=com
        GroupdSearchDB: ou=Groups,dc=maxcrc,dc=com

我有一个用于解析的类:

代码语言:javascript
复制
public class YamlConfiguraiton {
    private AuthenticationConfiguration AuthenticationConfig;

    public void setAuthenticationConfig(AuthenticationConfiguration AuthenticationConfig) {
        this.AuthenticationConfig = AuthenticationConfig;
    }

    public AuthenticationConfiguration getAuthenticationConfig() {
        return this.AuthenticationConfig;
    }
}

但是,当我跑的时候

代码语言:javascript
复制
try(InputStream in = new FileInputStream(new File(ymalPath))) {
            yamlConfig = yaml.loadAs(in, YamlConfiguraiton.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

发生以下错误:

代码语言:javascript
复制
Exception in thread "main" Cannot create property=AuthenticationConfig for JavaBean=com.ibm.entity.matching.common.bootstrap.YamlConfiguraiton@e7860081
 in 'reader', line 2, column 1:
    AuthenticationConfig:
    ^
Unable to find property 'AuthenticationConfig' on class: com.ibm.entity.matching.common.bootstrap.YamlConfiguraiton
 in 'reader', line 3, column 4:
       AuthencticationType: LDAP
       ^

    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:270)
    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:204)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:193)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:159)
    at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:146)
    at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:524)
    at org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:518)
    at com.ibm.entity.matching.bootstrap.EntityMatching.boot(EntityMatching.java:55)
    at com.ibm.entity.matching.bootstrap.EntityMatching.main(EntityMatching.java:35)
Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 'AuthenticationConfig' on class: com.ibm.entity.matching.common.bootstrap.YamlConfiguraiton
    at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:159)
    at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:148)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.getProperty(Constructor.java:287)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:208)
    ... 10 more

为什么它抱怨不能找到属性AuthenticationConfig,而AuthenticationConfig只是实例变量的名称?

在我将实例变量从“私有”更改为“公共”后,更新了,它们被SnakeYaml识别,但这不是我们所期望的。这些类不能被识别为JavaBean。

更新我找到了根本原因。这是命名惯例。如果希望SnakeYaml解析yaml文件,则必须遵守camelCase。setter和getter方法的名称也很重要。假设有一个名为ldapConfig的私有实例变量,那么它的getter和setter的名称必须是getLdapConfig和setLdapConfig,甚至getLDAPConfig和setLDAPConfig也不能工作。

EN

回答 1

Stack Overflow用户

发布于 2018-10-03 10:21:02

导致此错误的主要原因是您需要在POJO类(即YamlConfiguraiton)中定义Yaml文件中的所有属性。

可以使用下面的代码跳过未定义的属性。

代码语言:javascript
复制
Representer representer = new Representer();
            representer.getPropertyUtils().setSkipMissingProperties(true);
            Yaml yaml = new Yaml(new Constructor(YamlConfiguraiton.class), representer);

首先,在Yaml文件中将属性名重命名为camelCase。

请参阅以下代码:-

代码:-

代码语言:javascript
复制
public class YamlReadCustom {

    private static String yamlPath = "/authentication.yaml";

    public static void main(String[] args) {
        Representer representer = new Representer();
        representer.getPropertyUtils().setSkipMissingProperties(true);
        Yaml yaml = new Yaml(new Constructor(Authentication.class), representer);
        try(InputStream in = YamlReadCustom.class.getResourceAsStream (yamlPath)) {
            Authentication authentication = yaml.loadAs(in, Authentication.class);
            System.out.println(authentication.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Authentication:-

代码语言:javascript
复制
public class Authentication {
    private String authenticationConfig;
    private String authencticationType;
    private LdapConfig ldapConfig;

    //getters and setters
}

LdapConfig:-

代码语言:javascript
复制
public class LdapConfig {
    private String ldapUrl;
    private String connectionType;
    private Map<String, Object> ldapSecurityConfig;
    //getters and setters
}

authentication.yaml

代码语言:javascript
复制
authenticationConfig:
authencticationType: LDAP
ldapConfig:
  ldapUrl: ldap://localhost:389
  connectionType: simple
  ldapSecurityConfig:
    rootDn: cn=manager,dc=maxcrc,dc=com
    rootPassword: secret
    userSearchDn: ou=People,dc=maxcrc,dc=com
    groupdSearchDb: ou=Groups,dc=maxcrc,dc=com
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52619452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档