java - 从 YAML 文件 : No content to map due to end-of-input 读取时出现 JsonMappingException

标签 java jackson yaml

我一直在创建类,它将负责从 yml 文件获取我的所有设置。这个类看起来像这样:

public class TestsConfiguration extends Configuration {
    public TestsConfiguration() {
        this.sourceYAML = this.getClass().getResourceAsStream("test.yml");
    }
    public TestsConfiguration(File sourceYAML) throws FileNotFoundException {
        this.sourceYAML = new FileInputStream(sourceYAML);
    }

    public class PropertiesContainer {
        @JsonProperty
        private String test;

        public String getTest() {
            return test;
        }

        public void setTest(String test) {
            this.test = test;
        }
    }

    private final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    private final InputStream sourceYAML;

    public PropertiesContainer getProperties() throws IOException {
        final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        return mapper.readValue(sourceYAML, PropertiesContainer.class);
    }
}

但是我在 mapper.readValue(...) 方法中遇到异常

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input

这是我的 yml 文件:

test: "hello"

如何解决这个问题?

最佳答案

看起来 getProperties() 方法被调用了多次。

在构造函数中初始化的输入流字段。它有一个文件指针变量,每次读取时都会更新该变量。当输入流到达文件末尾时发生异常。

尝试将 YAML 解析移至构造函数。

关于java - 从 YAML 文件 : No content to map due to end-of-input 读取时出现 JsonMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35917559/

相关文章:

templates - 在 helm 图表中附加 yaml anchor

java - doAnswer 检查哪个类调用了特定函数?

java - 如何从安卓日历中获取日期?

java - jackson 即使使用相同的版本也会抛出 IllegalAccessError

java - 如何使用 Jersey 客户端使用 Facebook JSON

ruby-on-rails - 如何使用Rails使用两个关系i18n yaml文件以一种形式很好地显示?

java,paint Jpanel,它在另一个JPanel里面

c# - 是否有可能在 c# 中模仿这个 java 枚举代码

java - 使用格式自定义反序列化日期

c++ - 如何在 OpenCV 中以编程方式在 YAML 中编写注释?