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<>();
}
}
- 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; }
}
}
- 主要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);
}
}
我不知道为什么我的对象总是空的。我也用过这个在线转换器 https://jsonformatter.org/yaml-to-java 但我都没有得到好的结果。
感谢您的宝贵时间,对不起我的英语,这不是我的母语。
您的应用设置在 spring 配置中。
尝试在您的 AppProperties class
中将 @ConfigurationProperties(prefix = "app")
更改为 @ConfigurationProperties(prefix = "spring.app")
当我尝试在 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<>();
}
}
- 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; }
}
}
- 主要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);
}
}
我不知道为什么我的对象总是空的。我也用过这个在线转换器 https://jsonformatter.org/yaml-to-java 但我都没有得到好的结果。
感谢您的宝贵时间,对不起我的英语,这不是我的母语。
您的应用设置在 spring 配置中。
尝试在您的 AppProperties class
中将@ConfigurationProperties(prefix = "app")
更改为 @ConfigurationProperties(prefix = "spring.app")