Spring Boot - Configuration Value from yml is null

12,882

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!

Share:
12,882

Related videos on Youtube

Zelle
Author by

Zelle

Updated on June 04, 2022

Comments

  • Zelle
    Zelle almost 2 years

    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.

    • Anas
      Anas over 6 years
      can you try url: 'testUrl'
    • Zelle
      Zelle over 6 years
      Tryed with url: 'testUrl'. No changes.
    • pleft
      pleft over 6 years
      Have you defined getter and setter methods for url field in your AirtableProperties class?
    • Zelle
      Zelle over 6 years
      Yes, added them so its clear in the question.
    • pleft
      pleft over 6 years
      Can you run your app in debug mode and place a breakpoint in the setUrl method and examine the values there?
    • Barath
      Barath over 6 years
      I guess try setting to url from endpointURL in the setter
    • Zelle
      Zelle over 6 years
      I ran the app in debug mode. However it didnt hit the Breakpoint of setUrl. So I tried it with the setProxy Method. There it did stop. So the setUrl Method isnt called to set the Value am I right? What could be the cause?
    • Zelle
      Zelle over 6 years
      Changed the name of endpointUrl to url so it does match no changes.
    • PaulNUK
      PaulNUK over 6 years
      Check application.yaml doesn't have weird tabs in it for the URL property. Try running it with -Dairtable.url=someurl and see if that works Also inject in an Environment and then debug that to look at the property sources and see if airtable.url is there, or an alternative url property
    • Zelle
      Zelle over 6 years
      checked the yaml for weird tabs etc, no luck. Runnig it with -Dairtable.url=someurl didnt helped either.
    • PaulNUK
      PaulNUK over 6 years
      Why have you got quotes around this : proxy: "localhost:8095" ?
    • Zelle
      Zelle over 6 years
      it was suggested to try it with quotes because there is a : in there. I tested with single quotes double quotes etc but it doesent matter.
  • PaulNUK
    PaulNUK over 6 years
    In your example you're setting proxy to localhost:8095 whereas in the original post it's being set to "localhost:8095"' so that is different,
  • Barath
    Barath over 6 years
    @PaulNUK he edited the question after reading the comments posted by someone. see the time of answered vs question edited
  • PaulNUK
    PaulNUK over 6 years
    Ah OK, I suspect the quotes are messing it up, based on the fact that your version is working fine.