gpt4 book ai didi

java - Dropwizard 0.7.1 与 0.6.2 : different YML configuration parser?

转载 作者:行者123 更新时间:2023-11-30 11:16:31 25 4
gpt4 key购买 nike

我有这个 .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私有(private)列表桶 = Lists.newArrayList();公共(public)静态类桶 {

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

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

最佳答案

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

例子.yml

buckets:
- name: name1
properties:
prop1: moo
prop2: meep
prop3: momp
- name: name2
properties:
prop1: axe
prop2: farid
prop3: tom

示例配置.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:,请确保将所有这些嵌套在您的 Storage POJO 中。

关于java - Dropwizard 0.7.1 与 0.6.2 : different YML configuration parser?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24820123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com