Java – Spring Boot – Configuration Value from yml is null

javaspringspring-bootyaml

im loading a configuration yml using spring configuration annotations. Everything is working fine with 3 of the 4 values I configured. However the 4th Value is null.

Since the other Values are loading properly i dont think there is a configuration Error. Im clueless…

Here is my Code:

Yml-File

spring:
    profiles: test

airtable:
    api-key: xxx
    base: xxx
    proxy: "localhost:8095"
    url: testUrl

mail:
    subjectPrefix: R750Explorer develop - 

PropertiesClass

@Configuration
@ConfigurationProperties(prefix = "airtable")
public class AirtableProperties {

@NotNull
private String apiKey;

@NotNull
private String base;

private String proxy;

private String url;


public String getApiKey() {
    return apiKey;
}

public void setApiKey(String apiKey) {
    this.apiKey = apiKey;
}

public String getBase() {
    return base;
}

public void setBase(String base) {
    this.base = base;
}

public String getProxy() {
    return proxy;
}

public void setProxy(String proxy) {
    this.proxy = proxy;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}


}

Autowired them here

public class AirtableRepository {

private final org.slf4j.Logger log = 
LoggerFactory.getLogger(this.getClass());

private Base base = null;

@Autowired
private AirtableProperties prop;

Main Application File

@SpringBootApplication
@EnableCaching
@EnableScheduling
@EnableConfigurationProperties
public class Application extends SpringBootServletInitializer {


public static void main(String[] args) throws Exception {
.
.
.

public static void main(String[] args) throws Exception {

    SpringApplication.run(Application.class, args);
}
}

So I get the Values I define in api-key,base and proxy. However url is null.

========================= Edit =============================

Update. I use both a default application.yml and a profile specific application-test.yml

The above yml is the application-test yml. Heres the application.yml

spring:
    application:
        name: R750Explorer

    boot:
        admin:
            #url: http://localhost:8085       

    devtools:
        restart:
            additional-paths: src, target
            exclude: "**/*.log"

    mail:
        properties:
            mail:
                smp:
                   connectiontimeout: 5000
                   timeout: 3000
                   writetimeout: 5000

    mvc:
        view:
            prefix: /WEB-INF/jsp/
            suffix: .jsp

    output:
        ansi:
            enabled: ALWAYS

    profiles:
        #default: default
        #active: dev 

    airtable:
        api-key: none-default 

    mail:
        from-address: XXX
        to-address: XXX
        user: XXX
        password: XXX

server:
    address: 127.0.0.1
    #port: 9000
    compression:
        enabled: true
    session:
        cookie:
            #comment: # Comment for the session cookie.
            # domain: # Domain for the session cookie.
            http-only: true
            # -> ein Jahr / Maximum age of the session cookie in seconds.
            max-age: 31536000
            #name:  Session cookie name.
            #path: # Path of the session cookie.
            # "Secure" flag for the session cookie.
            secure: true    

logging:
    file: logs/r750explorer.log
    level:
        com:
            sybit: DEBUG

management:
    context-path: /manage
    security: 
        enabled: false
        # roles: SUPERUSER

security:
    user:
        #name: admin
        #password=****

Now heres the clue: If i add url: testurl in the default application.yml under airtable it writes the value. however it doesent in the application-test.yml. Although this is only the case for url not for proxy etc, they are working fine.

Best Answer

It works for me without any changes :

application.yml :

airtable:
    api-key: xxx
    base: xxx
    proxy: localhost:8095
    url: testUrl

POJO :

@Configuration
@ConfigurationProperties(prefix = "airtable")
public class AirtableProperties {

    @NotNull
    private String apiKey;

    @NotNull
    private String base;

    private String proxy;

    private String url;

    public String getApiKey() {
        return apiKey;
    }

    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    public String getProxy() {
        return proxy;
    }

    public void setProxy(String proxy) {
        this.proxy = proxy;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public String toString() {
        return "AirtableProperties{" +
                "apiKey='" + apiKey + '\'' +
                ", base='" + base + '\'' +
                ", proxy='" + proxy + '\'' +
                ", url='" + url + '\'' +
                '}';
    }

output :

   PROPS ARE AirtableProperties{apiKey='xxx', base='xxx', proxy='localhost:8095', url='testUrl'}