hunnyb
hunnyb

Reputation: 11

Unable to load properties from yaml file using spring boot

Below is my (policies.yml) yaml file in which I want to parse/map to classes through spring.

urls:
  - url: url1
    actions:
      - action: acton1
        domains:
          - domain: domain1
      - action: action2
        domains:
          - domain: domain1
  
  - url: url2
    actions:
      - action: acton1
        domains:
          - domain: domain1
      - action: action2
        domains:
          - domain: domain1

this is my configuration class to load yaml file.

@Component
@EnableConfigurationProperties
@PropertySource(value = "classpath:policies.yml")
@ConfigurationProperties("urls")
public class SecurityURLConfigs {   
    private List<Url> urls = null;  
}

public class Url{
   private String url;
   private List<Action> actions = null;
}

public class Action {
    private String action;  
    private List<Domain> domains = new ArrayList<Domain>(); 
}

public class Domain {
    private String domain;
}

but this configuration isn't loading properties from yaml file. Can anybody guide whats wrong in this code.

Upvotes: 1

Views: 1439

Answers (1)

Gordon from Blumberg
Gordon from Blumberg

Reputation: 199

You wrote your config file is named policies.yml, but in the code you try to load framepolicies.yml.

Also you set urls property as root, but try to get it inside class. I think you should either try to set @ConfigurationProperties("") or move the urls block inside another and set @ConfigurationProperties("another").

Upvotes: 2

Related Questions