Dropwizard 0.7.1 vs 0.6.2:不同的YML配置解析器?

时间:2014-07-18 08:07:00

标签: java jackson yaml dropwizard

我是这个.yml文件:

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 ImmutableMap<String, String> buckets = ImmutableMap.of();

出现此错误:

 testapp.yml has an error:   * Unrecognized field at: storage.buckets
     Did you mean?:
       - storageUri
       - storageAccessKey
       - storageSecretKey

使用第二种配置(仅适用):

@Valid @NotNull private List buckets = Lists.newArrayList(); public static class 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:
  * 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>

解析这个该死的存储配置的正确方法在哪里?

1 个答案:

答案 0 :(得分:1)

处理它的最好方法可能就像你的第二个例子。你可以做这样简单的事情(请注意,在我的.yml文件中,我在根目录中有buckets - 仅用于示例目的。)

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

另外,如果

,你的第三个适合我
one:
  bucket: one-buck-name
second:
  bucket: second-buck-name

处于根级别。如果您正在使用storage:,请务必将所有这些嵌套在您的存储POJO中。