Search code examples
javaspring-bootyamlpojomapper

yaml to java object, is always null


when I try to map yaml file in java object, but my java object is always null,I have this files

  • application yaml.

    spring:
       security:
          oauth2:
            client:
              registration:
                google:
                  clientId: {some client}
                  clientSecret: {some client secret}
                  redirectUri: "{baseUrl}/oauth2/callback/{registrationId}"
                  scope:
                    - email
                    - profile
       app:
         auth:
           tokenSecret: 04ca023b39512e46d0c2cf4b48d5aac61d34302994c87ed4eff225dcf3b0a218739f3897051a057f9b846a69ea2927a587044164b7bae5e1306219d50b588cb1
           tokenExpirationMsec: 864000000
           
           oauth2:
           # After successfully authenticating with the OAuth2 Provider,
           # we'll be generating an auth token for the user and sending the token to the
           # redirectUri mentioned by the client in the /oauth2/authorize request.
           # We're not using cookies because they won't work well in mobile clients.
           authorizedRedirectUris:
            - http://localhost:8080/oauth2/redirect

and this pojo


    @ConfigurationProperties(prefix = "app")
    @Data
    @AllArgsConstructor
    public class AppProperties {
    
        private final Auth auth = new Auth();
        private final OAuth2 oauth2 = new OAuth2();
        
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public static class Auth {
            private String tokenSecret;
            private long tokenExpirationMsec;
        }
    
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public static final class OAuth2 {
            private List<String> authorizedRedirectUris = new ArrayList<>();
        }
    }

  • ClientConfig.yaml

    urls:
      user: localhost:8080/


    @ConfigurationProperties()
    @PropertySource(value = "clientConfig.yaml")
    public class ClientConfig {
        private Urls urls;
    
        public Urls getUrls() { return urls; }
        public void setUrls(Urls value) { this.urls = value; }
        
        public static class Urls {
                private String user;
    
                public String getUser() { return user; }
                public void setUser(String value) { this.user = value; }
            }
    }

  • main class

    @SpringBootApplication(exclude = {
            DataSourceAutoConfiguration.class, 
            DataSourceTransactionManagerAutoConfiguration.class, 
            HibernateJpaAutoConfiguration.class
        })
    @EnableConfigurationProperties({AppProperties.class, ClientConfig.class})
    public class SecurityManagerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SecurityManagerApplication.class, args);
        }
    
    }

I have no idea about why my objects are always null. Also i have used this online converter https://jsonformatter.org/yaml-to-java but neither i have got good results.

thanks for your time and sorry for my english, It is not my native languaje.


Solution

  • Your app settings are in spring configuration.

    Try to change @ConfigurationProperties(prefix = "app") to @ConfigurationProperties(prefix = "spring.app") in your AppProperties class