2016-03-10 18 views
1

我一直在創建類,它將負責從yml文件中獲取所有設置。這個類看起來是這樣的:從YAML文件讀取時發生JsonMappingException:由於輸入結束而無法映射內容

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:沒有內容映射由於最終的輸入

這是我的YML文件:

test: "hello" 

我該如何解決這個問題?

回答

1

看起來像getProperties()方法正在被調用幾次。

輸入流字段在構造函數中初始化。它有一個文件指針變量,每次讀取它時都會更新。輸入流到達文件結尾時發生異常。

嘗試將YAML解析移到構造函數中。

相關問題