我一直在创建类,它将负责从 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/