SpringValidator

栏目: Java · 发布时间: 6年前

内容简介:注入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}'}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

程序与法治

程序与法治

孙洪坤 / 中国检察 / 2008-3 / 28.00元

《程序与法治》是系统和全面地研究程序与法治国家建设思想的学术专著,《程序与法治》对程序与法治的若干重大理论与现实问题进行了深入的探讨,如:从法社会学的视角研究程序正义在中国的可适应性问题;程序正义的中国语境;正当程序的宪政价值与构造;正当程序的文化底蕴;中国刑事程序正当化设计的标准、设计的基调、设计的视角;等等。尽管其中某些问题的研究尚待进一步深入,但这些问题的提出有利于开阔我们研究程序法理论的视......一起来看看 《程序与法治》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

SHA 加密
SHA 加密

SHA 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具