Dropwizard配置文件会自动将值转换为自定义对象。

huangapple go评论58阅读模式
英文:

Dropwizard conf file automatically convert value to custom object

问题

我有一个Dropwizard应用程序,它解析配置文件以构建应用程序的配置。

base.conf文件中有:

country: USA

在配置的Java文件中:

@NotNull
private MyObject country;

在这种情况下,MyObject不是一个枚举,而是一个普通的对象。

我该如何配置它,以便Dropwizard会根据我定义的某些逻辑自动将解析的字符串值转换为MyObject,就像这样:

MyObject convertStringToMyObject(String value) {
    if (value.equals("USA")) {
        return new MyObject("+1", "北美洲", "美国");
    }
}

显然,这只是我能想到的用于实现我想要的最简单的示例。

英文:

I have a dropwizard application that parses conf files to construct the application's configuration.

base.conf file has:

country: USA

in the configuration java file:

@NotNull
private MyObject country;

MyObject is not an enum in this case. It is a regular object.

How can I configure it to have dropwizard automatically convert the parsed string value to MyObject based on some logic I define, like:

MyObject convertStringToMyObject(String value) {
    if (value.equals("USA") {
        return new MyObject("+1", "North America", "USA");
    }
}

This is obviously just the simplest dumb sample I could think of for what I'm trying to achieve.

答案1

得分: 1

Sure, here are the translated parts:

(1) 使用配置中的枚举,并提供到 MyObject 类的映射,以控制根据国家驱动字段的实例化,这将清楚地显示支持的国家(以及您拥有映射的国家)。

public class MyConfig extends Configuration {

    enum Country {
        美国;

        MyObject toObject() {
            switch (this) {
                case 美国:
                    return new MyObject("美国电话区号", "北美洲"...);
            }
            ...
        }
    }
    
    public Country country;
}

(2) 注册自定义反序列化器。即使您不拥有 MyObject 类,您仍然可以在 ObjectMapper 上直接注册自定义反序列化器,或者在应用程序配置中的字段上使用 JsonDeserializer(using = MyObjectDeserializer) 注释(而不是在 MyObject 类声明上使用,正如您所说 - 您无法编辑它) - 示例:

public class MyConfig extends Configuration {

    @JsonDeserialize(using = MyObjectDeserializer.class)
    public MyObject country;
}
英文:

There are a couple of different ways you might want to consider when approaching:

(1) Use an enumeration in the configuration and then provide a mapping to the MyObject class which controls the instantiation of the fields driven by the country. This will make it clear which countries are supported (and for which you have mappings).

public class MyConfig extends Configuration {

    enum Country {
        USA;

        MyObject toObject() {
            switch (this) {
                case USA:
                    return new MyObject("+1", "North America"...);
            }
            ...
        }
    }
    
    public Country country;
}

(2) Register a custom deserialiser. Even if you don't own the MyObject class you will still be able to register a custom deserialiser either directly on the ObjectMapper or by using the JsonDeserializer(using = MyObjectDeserializer) annotation on the field in your application configuration (rather than on the MyObject class declaration which as you say - is not editable by you) - example:

public class MyConfig extends Configuration {

    @JsonDeserialize(using = MyObjectDeserializer.class)
    public MyObject country;
}

huangapple
  • 本文由 发表于 2023年6月13日 15:41:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定