gpt4 book ai didi

snakeyaml - 从 YAML 文件创建对象时出现问题

转载 作者:行者123 更新时间:2023-12-02 23:28:43 24 4
gpt4 key购买 nike

我只是尝试使用 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

最佳答案

如上所述here问题可能是由于缺少 arg 构造函数、setter/getter 对或字段本身而引起的。一旦您使类适应 Java bean 规范,它就会出色地工作。

关于snakeyaml - 从 YAML 文件创建对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36817171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com