当前位置: 首页 > 知识库问答 >
问题:

SnakeYaml“无法找到属性错误”

卫嘉佑
2023-03-14

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

#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

我有一个用于解析的类:

public class YamlConfiguraiton {
    private AuthenticationConfiguration AuthenticationConfig;

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

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

然而,当我跑的时候

try(InputStream in = new FileInputStream(new File(ymalPath))) {
            yamlConfig = yaml.loadAs(in, YamlConfiguraiton.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

发生以下错误:

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只是实例变量的名称?

更新在我将实例变量从“private”更改为“public”后,它们被SnakeYaml识别,但这并不是我们所能肯定的。类不被识别为JavaBean。

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

共有1个答案

费德宇
2023-03-14

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

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

Representer representer = new Representer();
            representer.getPropertyUtils().setSkipMissingProperties(true);
            Yaml yaml = new Yaml(new Constructor(YamlConfiguraiton.class), representer);

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

请参阅以下代码:-

代码:-

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();
        }
    }
}

身份验证:-

public class Authentication {
    private String authenticationConfig;
    private String authencticationType;
    private LdapConfig ldapConfig;

    //getters and setters
}

ldapconfig:-

public class LdapConfig {
    private String ldapUrl;
    private String connectionType;
    private Map<String, Object> ldapSecurityConfig;
    //getters and setters
}

authentication.yaml

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
 类似资料:
  • 我正试图在我的Android Studio 1.5.1中导入一个项目。我首先删除了'com'的一个错误。Android找不到应用程序,但出现了此错误。在此处输入图像描述 Gradle同步失败:在ProductFlavor_装饰的{name=main,dimension=null,minSdkVersion=DefaultApiVersion{mApiLevel=14,mCodename='null

  • 我有一个错误:C:\Users\juan_\AndroidStudioProjects\DermaApp\app\src\main\res\layout\activity_login.xml:33:aapt:error:找不到属性starticcondrawable(又名com.example.example:starticcondrawable)。 错误出现在XML:app:starticond

  • 执行任务:[: app: assembleDebug] 任务:应用:预构建最新任务:应用:预构建最新任务:应用:预构建最新任务:应用:CompiledBugAidl无源任务:应用:checkDebugManifest最新任务:应用:CompiledBugRenderScript无源任务:应用:GeneratedBugConfig Build最新任务:应用:PrepareNetjar最新任务:应用:

  • 我有一个本地的postgresql 10服务器(在9.6上测试了相同的版本,不要认为它与数据库有关,只与pgAdmin4有关,因为pgAdmin3工作得很好) 正如屏幕截图中所示,我试图在新安装时使用用户postgres连接到localhost db,但我一直收到以下消息:

  • 我有相当好的分级配置,这使一切都很好。但是我的多项目构建中的一个项目是从其馀的项目中派生出来的,所以我很乐意把它移到另一个git repo中,并配置子模块来处理它。 首先,我将Project及其资源移动到子文件夹中。在改变了梯度配置中的一些线后,它工作得很好。但后来我决定只为这个项目编写一个新的build.gradle,并将所有配置从主配置移到那里。 就在这里一切都停止了。当我尝试调用任何任务时,

  • 我目前完全无法解决Hibernate的问题,我得到的消息是: 在以下方法中: 但是我在SQL中没有看到任何错误,它在阿帕奇德比中运行得很好。 我已经在 StackOverflow 和其他地方尝试了一些方法,但没有什么可以解决这个问题。 一个简单的应用程序中有两个类: 和书: 每个映射到数据库表: 而书是: 谁能告诉我哪里出了问题? 如果SQL最终起作用,如果我使用正确的方法将输出转换为Person