使用名称中带下划线的Snakeyaml映射POJO



我有下面的配置文件,我试图将这个yaml文件映射到带有Snakeyml库的Java POJO类中。

consumers:
- acls:
- group: GRP-NAME-1
tags:
- CON-NAME
- group: GRP-NAME-2
tags:
- CON-NAME-TAG
oauth2_credentials:
- client_id: CRD-NAME
client_secret: xxxx
name: CRD-NAME
redirect_uris:
- http://xyz
tags:
- CON-NAME-TAG
username: CON-NAME
tags:
- CON-NAME-TAG

RootConfig.java

public class RootConfig {
private List<Consumer> consumers;
public List<Consumer> getConsumers() {
return consumers;
}
public void setConsumers(List<Consumer> consumers) {
this.consumers = consumers;
}
}

Consumer.java:

public class Consumer {
private List<Acl> acls;
private List<Oauth2Credential> oauth2Credentials;
private String username;
private List<String> tags;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<Acl> getAcls() {
return acls;
}
public void setAcls(List<Acl> acls) {
this.acls = acls;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
public List<Oauth2Credential> getOauth2Credentials() {
return oauth2Credentials;
}
public void setOauth2Credentials(List<Oauth2Credential> oauth2Credentials) {
this.oauth2Credentials = oauth2Credentials;
}
}

Oauth2Credential.java:

public class Oauth2Credential {
private String clientId;
private String clientSecret;
private String name;
private List<String> redirectUris;
private List<String> tags;
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getRedirectUris() {
return redirectUris;
}
public void setRedirectUris(List<String> redirectUris) {
this.redirectUris = redirectUris;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
}

Acl.java:

public class Acl {
private String group;
private List<String> tags;
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
}

ConfigLoader.java:

public class ConfigLoader {
public RootConfig load(String file) {
Yaml yaml = new Yaml(new Constructor(RootConfig.class));
InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream(file);
RootConfig rootConfig = yaml.load(inputStream);
System.out.println(rootConfig);
return rootConfig;
}
}

配置加载程序给我以下错误:

Caused by: Cannot create property=oauth2_credentials for JavaBean=uk.gov.hmrc.deck.config.tool.modal.Consumer@1554909b
in 'reader', line 9, column 5:
- oauth2_credentials:
^
Unable to find property 'oauth2_credentials' on class: uk.gov.hmrc.deck.config.tool.modal.Consumer
in 'reader', line 10, column 7:
- client_id: CRD-MDTP-BREATHINGS ... 
^
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:291)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:172)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:230)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:220)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequenceStep2(BaseConstructor.java:391)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequence(BaseConstructor.java:375)
at org.yaml.snakeyaml.constructor.Constructor$ConstructSequence.construct(Constructor.java:543)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:230)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:220)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.newInstance(Constructor.java:306)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:268)
... 10 more
Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 'oauth2_credentials' on class: uk.gov.hmrc.deck.config.tool.modal.Consumer
at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:158)
at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:148)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.getProperty(Constructor.java:310)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:231)
... 20 more

如果我删除oauth2_credentials元素,它似乎工作正常。我认为名称中的下划线导致了这个问题。知道怎么解决这个问题吗?

您需要按如下方式使用@YamlProperty(key = "oauth2_credentials")

public class Consumer {
private List<Acl> acls;
@YamlProperty(key = "oauth2_credentials")
private List<Oauth2Credential> oauth2Credentials;
private String username;
private List<String> tags;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<Acl> getAcls() {
return acls;
}
public void setAcls(List<Acl> acls) {
this.acls = acls;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
public List<Oauth2Credential> getOauth2Credentials() {
return oauth2Credentials;
}
public void setOauth2Credentials(List<Oauth2Credential> oauth2Credentials) {
this.oauth2Credentials = oauth2Credentials;
}
}

此外,解析时必须使用AnnotationAwareConstructor

public class ConfigLoader {
public RootConfig load(String file) {
Yaml yaml = new Yaml(new AnnotationAwareConstructor(RootConfig.class));
InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream(file);
RootConfig rootConfig = yaml.load(inputStream);
System.out.println(rootConfig);
return rootConfig;
}
}

最新更新