problems creating object from YAML file

user2395365 picture user2395365 · Apr 24, 2016 · Viewed 9.2k times · Source

I'm simply trying to manually read a dropwizard configuration file with SnakeYAML

My code looks like:

FileReader reader = new FileReader("/Users/novotny/IdeaProjects/fingage-platform/config-local.yml");
Yaml yaml = new Yaml();
FingageConfiguration config = yaml.loadAs(reader, FingageConfiguration.class);
System.err.println(config);

FingageConfiguration looks like:

public class FingageConfiguration extends Configuration {

    @Valid
    @NotNull
    private String env;

    @JsonProperty("env")
    public String getEnv() {
        return env;
    }

And my config-local.yml looks like:

env: local

server:
  requestLog:
  timeZone: UTC
applicationConnectors:
  - type: http
    port: 9370
adminConnectors:
  - type: http
    port: 9371

I don't quite understand why Dropwizard has no difficulty in constructing an object, but I can't seem to do it with SnakeYAML or am I doing something wrong? I get the following error below:

Exception in thread "main" Cannot create property=server for JavaBean=FingageConfiguration{server=DefaultServerFactory{applicationConnectors=[io.dropwizard.jetty.HttpConnectorFactory@42d8062c], adminConnectors=[io.dropwizard.jetty.HttpConnectorFactory@6043cd28], adminMaxThreads=64, adminMinThreads=1, applicationContextPath=/, adminContextPath=/}, logging=DefaultLoggingFactory{level=INFO, loggers={}, appenders=[io.dropwizard.logging.ConsoleAppenderFactory@77f99a05]}}
in 'reader', line 1, column 1:
env: "local"
^
Unable to find property 'server' on class: com.fingage.FingageConfiguration
in 'reader', line 4, column 5:
    requestLog:
    ^

at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:308)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:189)
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:341)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:141)
at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:127)
at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450)
at org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:410)
at com.fingage.service.TestYaml.main(TestYaml.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 'server' on class: com.fingage.FingageConfiguration
at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:132)
at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:121)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.getProperty(Constructor.java:318)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:240)
... 13 more

Answer

Ivan picture Ivan · Oct 21, 2016

As explained here problems may be caused by the absence of no arg constructors, setter/getter pairs or fields by themself. As soon as you adapt you classes to Java bean spec it works brilliantly.