我尝试将 javax.validation.constraints.NotNull 与 org.springframework.validation.beanvalidation.MethodValidationPostProcessor.MethodValidationPostProcessor 一起使用,但未在同一类中按预期运行。拜托,你能告诉我为什么吗?
方法验证配置类:
@Configuration
@ComponentScan({ "com.mypackage" })
public class MethodValidationConfig {
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
}
验证实用程序类:
@Component
@Validated
@Slf4j
public class ValidationUtils {
public void test1(@NotNull String[] test) {
if(test == null) {
log.error("Test is null!!!");
}
}
public void test2() {
test1(null);
}
}
测试应用类:
@EnableAsync
@EnableTransactionManagement
@SpringBootApplication
@Slf4j
public class TestApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(TestApplication.class, args);
ValidationUtils validationUtils = context.getBean(ValidationUtils.class);
try {
validationUtils.test1(null);
} catch (Exception e) {
log.info("OK");
}
try {
validationUtils.test2();
} catch (Exception e) {
log.info("OK");
}
}
}
com.mypackage.TestApplication:好的 com.mypackage.validationUtils:测试为空!!
小唯快跑啊
相关分类