简体   繁体   中英

Annotations when parsing YAML file to Configuration subclass in Dropwizard

In Dropwizard, I have a Configuration subclass which looks something like this:

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 ?

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.

EDIT: I am using Dropwizard version 0.7.0, if relevant.

@JsonCreator can be annotated only one of 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.
In the case of Dropwizard, you will not need an annotation because it will be unnecessary to instantiate the Configuration class yourself.

No, you don't need to. I think, you don't even need those @JsonProperty s. Yaml parser always tries getters and setters and then constructors using the field/parameter names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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