英文:
Is it possible to combine spring boot @Value with javax.validation.constraints?
问题
以下是翻译好的内容:
我想要在 application.properties
文件中验证数值。
我使用 @Value 进行注入,同时在一个被 @Configuration
注解的类中,对方法参数使用 @NotBlank 进行验证。
createSslContext(
@Value("#{systemProperties['javax.net.ssl.trustStore']}")
@NotBlank(message = "需要Truststore")
String path)
我已经添加了 hibernate-validator
和 hibernate-validator-annotation-processor
属性。使用的是 Spring 2.2.0.RELEASE 版本。
但是这并未生效。数值被注入,但未被验证。我漏掉了什么?
英文:
I would like to validate values in application.properties
.
I use @Value to inject and @NotBlank to validate the method parameter in a class annotated with @Configuration
.
createSslContext(
@Value("#{systemProperties['javax.net.ssl.trustStore']}")
@NotBlank(message = "Truststore is needed")
String path)
I have included the hibernate-validator
and the hibernate-validator-annotation-processor
properties. Using Spring 2.2.0.RELEASE.
It doesn't work. The value is injected, but not validated. What am I missing?
答案1
得分: 1
添加 @Validated
到你的配置类中。
@Configuration
@Validated
public class MyConfig {
@Bean
MyClass createSslContext(
@Value("#{systemProperties['javax.net.ssl.trustStore']}")
@NotBlank(message = "Truststore is needed") final
String path) {
...
}
}
英文:
Add @Validated
to your configuration class.
@Configuration
@Validated
public class MyConfig {
@Bean
MyClass createSslContext(
@Value("#{systemProperties['javax.net.ssl.trustStore']}")
@NotBlank(message = "Truststore is needed") final
String path) {
...
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论