簡體   English   中英

Yaml文件值未在Java中使用傑克遜加載

[英]Yaml file value is not loading using jackson in java

我想加載一個Yaml文件並將其存儲在Config.java中。

這是我的yaml文件:(更大)。我提供了一個簡化版本)

---
application:
  admin:
    jobInterValTime: 1440
    customer: lc
system:
  mongo:
    host: localhost
    port: 27017
    dbName: LC_Test
    collections:
      groupsCollection: groups
      membershipCollection: memberships
      personsCollection: persons

這是Config.java:

public class Config {
  private Application application;
  private System system;
  //Getter setter
}

Application.java

public class Application {
    private Admin admin;    
    //Getter Setter
}

管理員程序

public class Admin {
    private String jobInterValTime;
    private String customer;
    //Getter Setter
}

System.java

public class System {

    private Mongo mongo;       
    //Getter Setter
}

Mongo.java

public class Mongo {
    private String host;
    private String port;
    private String dbName;
    private Map<String, String> collections;
    //Getter Setter
}

但是, applicationsystem內部的對象Config.java即將null.No異常發生。 有人可以幫忙嗎?

這是我寫的。

Config config = null;
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
try{
    config = objectMapper.readValue(new File("src/test/java/resources/test1.yaml"), Config.class);
    //System.out.println(application.getAdmin().getCustomer());
    // System.out.println(unidataConfig.getApplication().getAdmin().getCustomer());

} catch(Exception e) {
    e.printStackTrace();
}

我不知道根本原因是什么,代碼看起來不錯。 但是您可以嘗試做的一件事是首先讀取MapJsonNode內容,並查看結構的外觀。 可能不匹配。

我解決了問題。 這個錯誤非常愚蠢。 在一個setter方法中,我這樣寫: var1 = var1而不是this.var1 = var1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM