【问题标题】:Dropwizard 0.7.1 vs 0.6.2: different YML configuration parser?Dropwizard 0.7.1 vs 0.6.2:不同的 YML 配置解析器?
【发布时间】:2014-07-18 08:06:59
【问题描述】:

我有这个 .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

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

@有效 @NotNull 私有列表存储桶 = Lists.newArrayList(); 公共静态类桶{

@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>

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

【问题讨论】:

    标签: java jackson yaml dropwizard


    【解决方案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 中。

    【讨论】:

      猜你喜欢
      • 2014-09-06
      • 1970-01-01
      • 2018-03-22
      • 2017-08-27
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多