validation


License

License

GroupId

GroupId

com.github.msimw
ArtifactId

ArtifactId

validation
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

jar
Description

Description

validation
validation
Project URL

Project URL

https://github.com/msimw/validation
Source Code Management

Source Code Management

https://github.com/msimw/validation

Download validation

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.msimw/validation/ -->
<dependency>
    <groupId>com.github.msimw</groupId>
    <artifactId>validation</artifactId>
    <version>1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.msimw/validation/
implementation 'com.github.msimw:validation:1.0'
// https://jarcasting.com/artifacts/com.github.msimw/validation/
implementation ("com.github.msimw:validation:1.0")
'com.github.msimw:validation:jar:1.0'
<dependency org="com.github.msimw" name="validation" rev="1.0">
  <artifact name="validation" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.msimw', module='validation', version='1.0')
)
libraryDependencies += "com.github.msimw" % "validation" % "1.0"
[com.github.msimw/validation "1.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.slf4j : slf4j-log4j12 jar 1.6.6
org.apache.commons : commons-lang3 jar 3.3.2
org.springframework : spring-context jar 4.2.2.RELEASE
org.aspectj : aspectjweaver jar 1.8.7

runtime (1)

Group / Artifact Type Version
cglib : cglib jar 2.2.2

test (1)

Group / Artifact Type Version
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

关于spring.validation 通用数据校验工具的使用说明

Maven

<dependency>
    <groupId>com.github.msimw</groupId>
    <artifactId>validation</artifactId>
    <version>1.0</version>
</dependency>

版本更新说明

v1.2 新增对dto字段校验继承。即子类可以直接继承父类的校验。

1.如何使用

1.再需要参数校验的工程引入classpath:validation/applicationContext-validation.xml
2.在需要校验的类或接口上加入@Validated
3.对于引用类型的校验,在参数上加@Validated(然后在类的属性上加校验注解)

1.默认有八种校验器(都是可继承的)

  @NotEmpty 为空判断
  @Length 长度校验
  @Pattern 正则校验
  @ChineseCharacters 中文校验
  @ChineseIdCard 中国身份证校验
  @Email 邮件校验
  @EnglishCharacters 英文字母校验
  @Number 数字

2.注解公共属性:messageCode,message,groups

  1.messageCode:消息码(当messageCode有值的情况下,会读取classpath:validation/validation.properties文件,否则读取message(message有默认的提示消息)
  2.message:消息
  3.groups:校验分组

2.关于扩展自定义校验规则

 1.新建一个校验规则注解(必须要有上述公共属性)
 2.继承AbstractValidationHandler或实现ValidationHandler类实现校验规则,使用ValidationHandlerExpand类来扩展新的校验规则(建议继承AbstractValidationHandler)

Demo:

<bean class="ValidationHandlerExpand">
    <property name="validationHandlers">
        <list>
            <bean class="NotNullHandler"></bean>
        </list>
    </property>
</bean>

3.建议

当系统某一校验规则多次出现的情况下建议扩展校验器。

4.Demo

1.代码示例
       
      @Validated
      public class DemoServiceImpl implements IDemoService {
      
          @Override
          public void add(@NotEmpty String id) {
      
          }
      
          @Override
          public void add1(@Validated Demo demo) {
      
          }
      }
       
       
2.配置文件
       
      <import resource="classpath:validation/applicationContext-validation.xml"></import>
      <bean id="demoService" class="com.msimw.demo.service.DemoServiceImpl"></bean>

Versions

Version
1.0