简体   繁体   English

在Dropwizard中将YAML文件解析为Configuration子类时的注释

[英]Annotations when parsing YAML file to Configuration subclass in Dropwizard

In Dropwizard, I have a Configuration subclass which looks something like this: 在Dropwizard中,我有一个Configuration子类,看起来像这样:

public class UserApplicationConfiguration extends Configuration {

    ...

    public UserApplicationConfiguration(
        @JsonProperty("externalServerUrl") String externalServerUrl,
        @JsonProperty("externalServerPort") int externalServerPort
    ) {
        this.externalServerUrl = externalServerUrl;
        this.externalServerPort = externalServerPort;
    }

    ...

}

The question is; 问题是; should I annotatate the constructor with @JsonCreator ? 我应该用@JsonCreator注释构造@JsonCreator吗?

The code works as is and the YAML file is correctly parsed but I think the constructor looks like a creator and feel that maybe it should be annotated as such. 该代码按原样工作,并且YAML文件已正确解析,但我认为构造函数看起来像创建者,并认为应该对其进行注释。

EDIT: I am using Dropwizard version 0.7.0, if relevant. 编辑:我正在使用Dropwizard版本0.7.0,如果相关。

@JsonCreator can be annotated only one of Constructor. @JsonCreator只能注释一个Constructor。
And , if there is only one constructor , there will not be need annotation. 并且,如果只有一个构造函数,则不需要注释。

So If you need some constructors, you should annotate only one constructor for jackson. 因此,如果需要一些构造函数,则只应为jackson注释一个构造函数。
In the case of Dropwizard, you will not need an annotation because it will be unnecessary to instantiate the Configuration class yourself. 对于Dropwizard,您将不需要注释,因为您不必自己实例化Configuration类。

No, you don't need to. 不,您不需要。 I think, you don't even need those @JsonProperty s. 我认为,您甚至不需要那些@JsonProperty Yaml parser always tries getters and setters and then constructors using the field/parameter names. Yaml解析器始终尝试使用获取器和设置器,然后使用字段/参数名称尝试构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM