3
votes

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.

2
can you try url: 'testUrl'Anas
Tryed with url: 'testUrl'. No changes.Zelle
Have you defined getter and setter methods for url field in your AirtableProperties class?pleft
Yes, added them so its clear in the question.Zelle
Can you run your app in debug mode and place a breakpoint in the setUrl method and examine the values there?pleft

2 Answers

0
votes

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'}
0
votes

So guys i found the answer. As I already told in the question im using a default application.yml and a application-test.yml.

These 2 were in seperate resource folders ( A copy of the test.yml with a syntax-Error was in the src/resource folder ).

After removing the Copy and implementing this structure everything worked:

| src/resources/

|--> application.yml

| test/resources/

|--> application-test.yml

Because of the Syntax-Error and the false placement of the Files it didnt worked out. Thanks for all the Helpers!