内容简介:注入Validator,调用
注入Validator,调用 javax.validation.Validator#validate
即可
示例代码
package bj.valid; import lombok.AllArgsConstructor; import lombok.Data; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationListener; import javax.annotation.Resource; import javax.validation.ConstraintViolation; import javax.validation.Valid; import javax.validation.Validator; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import java.util.Collections; import java.util.List; import java.util.Set; /** * Created by BaiJiFeiLong@gmail.com at 2018/12/13 下午2:49 */ @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class ValidApp implements ApplicationListener<ApplicationReadyEvent> { @Data @AllArgsConstructor static class User { private int id; @NotEmpty private String username; @Valid private List<Bankcard> bankcards; } @Data @AllArgsConstructor private static class Bankcard { private int id; @NotEmpty private String name; } @Resource private Validator validator; public static void main(String[] args) { SpringApplication.run(ValidApp.class, args); } @Override public void onApplicationEvent(@NotNull ApplicationReadyEvent event) { Set<ConstraintViolation<User>> violations = validator.validate(new User(1, "", Collections.singletonList(new Bankcard(1, "")))); System.out.println(violations.size()); violations.forEach(System.out::println); } }
控制台输出
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.0.RELEASE) 2018-12-13 15:06:08.153 INFO 555 --- [ main] bj.valid.ValidApp : Starting ValidApp on MacBook-Air-2.local with PID 555 (/Users/yuchao/temp/java/hellomaven/target/classes started by yuchao in /Users/yuchao/temp/java/hellomaven) 2018-12-13 15:06:08.161 INFO 555 --- [ main] bj.valid.ValidApp : No active profile set, falling back to default profiles: default 2018-12-13 15:06:09.826 INFO 555 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode! 2018-12-13 15:06:09.835 INFO 555 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode. 2018-12-13 15:06:09.909 INFO 555 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 34ms. Found 0 repository interfaces. 2018-12-13 15:06:11.584 INFO 555 --- [ main] bj.valid.ValidApp : Started ValidApp in 4.916 seconds (JVM running for 7.047) 2 ConstraintViolationImpl{interpolatedMessage='不能为空', propertyPath=bankcards[0].name, rootBeanClass=class bj.valid.ValidApp$User, messageTemplate='{javax.validation.constraints.NotEmpty.message}'} ConstraintViolationImpl{interpolatedMessage='不能为空', propertyPath=username, rootBeanClass=class bj.valid.ValidApp$User, messageTemplate='{javax.validation.constraints.NotEmpty.message}'}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
跨境电商——阿里巴巴速卖通宝典
速卖通大学 编著 / 电子工业出版社 / 2015-1 / 69.00元
跨境电商作为中国电子商务发展的最新趋势,受到了全社会越来越多的重视,大量中国卖家借助阿里巴巴速卖通平台,将产品直接售卖到全球的消费者手中,通过这条短得不能再短的交易链,获得了丰厚的回报。 但同时,跨境电商这一贸易形式,对卖家的综合素质要求比较高:卖家要对海外市场比较熟悉,对跨境物流有所把握,能够用外语进行产品介绍和客户交流,通过跨境结算拿到货款……诸如此类的门槛,让不少新卖家心生畏难,而所有......一起来看看 《跨境电商——阿里巴巴速卖通宝典》 这本书的介绍吧!