简体   繁体   English

Yaml文件值未在Java中使用杰克逊加载

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

I want to load a yaml file & store it in Config.java. 我想加载一个Yaml文件并将其存储在Config.java中。

Here is my yaml file: (Its much bigger. I am giving a simplified version) 这是我的yaml文件:(更大)。我提供了一个简化版本)

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

Here is Config.java: 这是Config.java:

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

Application.java Application.java

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

Admin.java 管理员程序

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

System.java System.java

public class System {

    private Mongo mongo;       
    //Getter Setter
}

Mongo.java Mongo.java

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

But the application & system object inside Config.java is coming null.No exception is happening. 但是, applicationsystem内部的对象Config.java即将null.No异常发生。 Can anybody help? 有人可以帮忙吗?

Here is what I have written. 这是我写的。

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();
}

I don't know the root cause here, code looks good. 我不知道根本原因是什么,代码看起来不错。 But one thing you could try is to read contents as Map or JsonNode first, and see how structure looks like. 但是您可以尝试做的一件事是首先读取MapJsonNode内容,并查看结构的外观。 There may well be a mismatch. 可能不匹配。

I solved the problem. 我解决了问题。 The mistake was very stupid. 这个错误非常愚蠢。 In one setter method I have written like this: var1 = var1 instead of this.var1 = var1 . 在一个setter方法中,我这样写: var1 = var1而不是this.var1 = var1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM