Search code examples
javajacksonyamlpojosnakeyaml

Yaml file value is not loading using jackson in java


I want to load a yaml file & store it in Config.java.

Here is my yaml file: (Its much bigger. I am giving a simplified version)

---
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:

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

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

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
}

But the application & system object inside Config.java is coming null.No exception is happening. 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();
}

Solution

  • 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.