Java Dropwizard 0.7.1与0.6.2:不同的YML配置解析器?
我有一个.yml文件:Java Dropwizard 0.7.1与0.6.2:不同的YML配置解析器?,java,jackson,yaml,dropwizard,Java,Jackson,Yaml,Dropwizard,我有一个.yml文件: template: Hello, testapp! storage: storageUri: **************** storageAccessKey: **************** storageSecretKey: ******************* buckets: "one": one-buck-name "second": second-buck-name buckets: - tag: one
template: Hello, testapp!
storage:
storageUri: ****************
storageAccessKey: ****************
storageSecretKey: *******************
buckets:
"one": one-buck-name
"second": second-buck-name
buckets:
- tag: one
name: one-buck-name
- tag: second
name: second-buck-name
one:
bucket: one-buck-name
second:
bucket: second-buck-name
在“storageSecretKey”下面,我有三种不同的方法来定义存储桶的配置。
所以我在用java解析这些配置时遇到了很多问题
对于第一个配置,我使用了:
使用第二种配置(仅适用于以下情况):
@有效的
@NotNull
private List bucket=Lists.newArrayList();
公共静态类Bucket{
@Valid
@NotNull
@JsonProperty
private String tag;
@Valid
@NotNull
@JsonProperty
private String name;
public Bucket(){}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
最后,对于第三种配置:
@Valid
@NotNull
@JsonProperty
private One one;
@Valid
@NotNull
@JsonProperty
private Second second;
public class One {
@Valid
@NotNull
@JsonProperty
private String bucket;
public String getBucket() {
return bucket;
}
}
public class Second {
@Valid
@NotNull
@JsonProperty
private String bucket;
public String getBucket() {
return bucket;
}
}
出现此错误时:
testapp.yml has an error: * Unrecognized field at: storage.buckets
Did you mean?:
- storageUri
- storageAccessKey
- storageSecretKey
testapp.yml has an error:
* Failed to parse configuration at: storage.one.bucket; Can not deserialize instance of java.lang.String out of END_OBJECT token
at [Source: N/A; line: -1, column: -1] (through reference chain: com.testapp.configurations.AppConfiguration["storage"]->com.testapp.configurations.StorageConfiguration["chat"]->com.testapp.configurations.One["one"])
注意:第三种配置与dropwizard 0.6.2完美配合
最新资料:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.1</version>
</dependency>
com.fasterxml.jackson.core
杰克逊注释
2.4.1
解析这个该死的存储配置的正确方法在哪里?处理它的最佳方法可能与您的第二个示例类似。您可以做这样简单的事情(请注意,在我的.yml文件中,我在根目录下有
bucket
——仅用于示例目的)
example.yml
buckets:
- name: name1
properties:
prop1: moo
prop2: meep
prop3: momp
- name: name2
properties:
prop1: axe
prop2: farid
prop3: tom
SampleConfiguration.java
public class SampleConfiguration extends Configuration
{
@NotNull
@JsonProperty
public List<Bucket> buckets;
public static class Bucket {
@NotNull
@JsonProperty
private String name;
@NotNull
@JsonProperty
private Map<String, String> properties;
public String getBucketName() {
return this.name;
}
public Map<String, String> getProperties() {
return this.properties;
}
}
}
位于根级别。如果您使用的是存储:
,请确保将所有这些嵌套在存储POJO中
public class SampleConfiguration extends Configuration
{
@NotNull
@JsonProperty
public List<Bucket> buckets;
public static class Bucket {
@NotNull
@JsonProperty
private String name;
@NotNull
@JsonProperty
private Map<String, String> properties;
public String getBucketName() {
return this.name;
}
public Map<String, String> getProperties() {
return this.properties;
}
}
}
one:
bucket: one-buck-name
second:
bucket: second-buck-name