簡體   English   中英

yaml 到 java 對象,始終為 null

[英]yaml to java object, is always null

當我嘗試在 java 對象中映射 yaml 文件,但我的 java 對象始終為空時,我有這個文件

  • 應用程序 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

還有這個 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<>();
        }
    }

  • 客戶端配置文件

    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; }
            }
    }

  • 主班

    @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);
        }
    
    }

我不知道為什么我的對象總是為空。 我也使用了這個在線轉換器https://jsonformatter.org/yaml-to-java但我都沒有得到好的結果。

感謝您的時間和抱歉我的英語,這不是我的母語 languaje。

您的應用程序設置采用 spring 配置。

嘗試在 AppProperties 類@ConfigurationProperties(prefix = "spring.app") @ConfigurationProperties(prefix = "app")更改為@ConfigurationProperties(prefix = "spring.app")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM