KIC
KIC

Reputation: 6121

Springboot yaml properties as map is not working

I am using Spring boot 1.5.9. I can not get this simple configuration class to work. What am I missing here?

yaml file:

lala:
  jobs:
    foo1: bar
    foo2: bar
  foo: lala

Configuration class:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.HashMap;
import java.util.Map;

@Configuration
@ConfigurationProperties(prefix = "lala", ignoreUnknownFields = false)
@PropertySource("classpath:jobs.yml")
public class Jobs {
    private Map<String, String> jobs = new HashMap<>();
    private String foo;

    public Map<String, String> getJobs() {
        return jobs;
    }

    public void setJobs(Map<String, String> jobs) {
        this.jobs = jobs;
    }

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    @Override
    public String toString() {
        return "Jobs{" +
                "jobs=" + jobs +
                ", foo='" + foo + '\'' +
                '}';
    }
}

I keep getting: Jobs{jobs={}, foo='null'}

Upvotes: 0

Views: 1840

Answers (1)

Makoto
Makoto

Reputation: 106430

You can't load YAML the way you're doing it now.

YAML files can’t be loaded via the @PropertySource annotation. So in the case that you need to load values that way, you need to use a properties file.

The main alternative is to use an application.yml file instead of a dedicated jobs.yml file. If you must have separation, then your only option is to use a properties file.

Upvotes: 1

Related Questions