我只是想用SnakeYAML手动读取dropwizard配置文件
我的代码如下:
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看起来像:
public class FingageConfiguration extends Configuration {
@Valid
@NotNull
private String env;
@JsonProperty("env")
public String getEnv() {
return env;
}
我的config-local.yml看起来像:
env: local
server:
requestLog:
timeZone: UTC
applicationConnectors:
- type: http
port: 9370
adminConnectors:
- type: http
port: 9371
我不太明白为什么Dropwizard在构造对象时没有任何困难,但我似乎无法用SnakeYAML做到这一点,或者我做错了什么?我在下面收到以下错误:
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
答案 0 :(得分:0)
正如here所解释的那样,问题可能是由于没有arg构造函数,setter / getter对或者它们自己的字段。只要您将类调整为Java bean规范,它就能很好地运行。
答案 1 :(得分:0)
正如堆栈跟踪所说,您缺少属性FingageConfiguration.server
。堆栈跟踪上方的消息只是一团糟。