1

I'm trying to setup and use a YAML as config file in my Spring Boot 1.5.1 project.

My YAML file looks like the following:

hue:
    user: cdKjsOQIRY8hqweAasdmx-WMsn
    ip: "http://192.168.1.69"
    scenes:
        sunstatus:
            enabled: true
            id: 93yv8JekmAneCU9
            group: 1
        disable:
            enabled: true
            id: 93yv8JekmAneCU9
            group: 6

It works perfectly fine to read hue.getUser(). However, hue.getScenes() returns null for some reason. My Java code for the Hue Config looks like the following:

@Configuration
@ConfigurationProperties(prefix = "hue")
public class Hue {
    private String user;
    private String ip;
    private Scenes scenes;

    /*
     * Getters and setters of course
     */

    public class Scenes {
        private Sunstatus sunstatus;
        private Disable disable;

        /*
         * Getters and setters
         */

        public class Sunstatus {
            private boolean enabled;
            private String id;
            private String group;

            /*
             * Getters and setters
             */
        }

        public class Disable {
            private boolean enabled;
            private String id;
            private String group;

            /*
             * Getters and setters
             */
        }
    }
}

I've tried as well to annotate the each class with prefix as well, both in the format of hue.scenes.sunstatus, scenes.sunstatus and just sunstatus as well.

Additionally I also tried to use the @Value annotation a bit without any luck.

It's the same results if I keep the data in application.yml or in an external file. Can always only reach getUser().

What am I doing wrong here?

Viktor Plane
  • 127
  • 3
  • 16

2 Answers2

0

I see you are using public non-inner classes for nested configuration, so you should add the @NestedConfigurationProperty instead:

public class Scenes {

    @NestedConfigurationProperty
    private Sunstatus sunstatus;

    @NestedConfigurationProperty
    private Disable disable;

Nested properties

You can use the @NestedConfigurationProperty annotation on a field to indicate that a regular (non-inner) class should be treated as if it were nested.

So either add the annotations (if you plan on using the classes elsewhere) or make them public static.

0

try this.

@Configuration
@ConfigurationProperties(prefix = "hue")
public class Hue {
    private String user;
    private String ip;
    private Scenes scenes = new Scenes();

    /*
     * Getters and setters of course
     */

    public class Scenes {
        private Sunstatus sunstatus = new Sunstatus();
        private Disable disable = new Disable();

        /*
         * Getters and setters
         */

        public class Sunstatus {
            private boolean enabled;
            private String id;
            private String group;

            /*
             * Getters and setters
             */
        }

        public class Disable {
            private boolean enabled;
            private String id;
            private String group;

            /*
             * Getters and setters
             */
        }
    }
}
Seamas
  • 1,041
  • 7
  • 13