zer*_*_yu 5 java yaml snakeyaml
这是我的 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
Run Code Online (Sandbox Code Playgroud)
我有一个用于解析的类:
public class YamlConfiguraiton {
private AuthenticationConfiguration AuthenticationConfig;
public void setAuthenticationConfig(AuthenticationConfiguration AuthenticationConfig) {
this.AuthenticationConfig = AuthenticationConfig;
}
public AuthenticationConfiguration getAuthenticationConfig() {
return this.AuthenticationConfig;
}
}
Run Code Online (Sandbox Code Playgroud)
然而,当我跑
try(InputStream in = new FileInputStream(new File(ymalPath))) {
yamlConfig = yaml.loadAs(in, YamlConfiguraiton.class);
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
发生以下错误:
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
Run Code Online (Sandbox Code Playgroud)
为什么它抱怨找不到属性 AuthenticationConfig 而 AuthenticationConfig 只是实例变量的名称?
更新 在我将实例变量从“私有”更改为“公共”之后,它们被 SnakeYaml 识别,但这不是我们所期望的。这些类不被识别为 JavaBean。
更新 我找到了根本原因。这是命名约定。如果您希望 SnakeYaml 解析您的 yaml 文件,则必须遵守 camelCase。setter 和 getter 方法的名称也很重要。假设有一个名为 ldapConfig 的私有实例变量,那么它的 getter 和 setter 的名称必须是 getLdapConfig 和 setLdapConfig,即使 getLDAPConfig 和 setLDAPConfig 也不起作用。
错误的主要原因是您需要在 POJO 类(即YamlConfiguraiton)中定义 Yaml 文件中存在的所有属性。
您可以使用下面的代码来跳过未定义的属性。
Representer representer = new Representer();
representer.getPropertyUtils().setSkipMissingProperties(true);
Yaml yaml = new Yaml(new Constructor(YamlConfiguraiton.class), representer);
Run Code Online (Sandbox Code Playgroud)
首先,将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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
验证:-
public class Authentication {
private String authenticationConfig;
private String authencticationType;
private LdapConfig ldapConfig;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
Ldap配置:-
public class LdapConfig {
private String ldapUrl;
private String connectionType;
private Map<String, Object> ldapSecurityConfig;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
身份验证.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
Run Code Online (Sandbox Code Playgroud)