web-boot-validation

An auto configuration framework for api parameter validation.

License

License

GroupId

GroupId

io.github.wilson-he
ArtifactId

ArtifactId

web-boot-validation
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

web-boot-validation
An auto configuration framework for api parameter validation.
Project URL

Project URL

https://github.com/Wilson-He/web-boot-validation
Source Code Management

Source Code Management

https://github.com/Wilson-He/web-boot-validation

Download web-boot-validation

How to add to project

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

Dependencies

compile (3)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.22
org.apache.commons : commons-lang3 jar 3.8
org.hibernate.validator : hibernate-validator jar 6.0.13.Final

provided (4)

Group / Artifact Type Version
org.springframework : spring-web jar 5.0.5.RELEASE
org.springframework.boot : spring-boot-autoconfigure jar 2.0.1.RELEASE
org.springframework : spring-webmvc jar 5.0.6.RELEASE
org.slf4j : slf4j-api jar 1.7.25

Project Modules

There are no modules declared in this project.

web-common-validation

基于Spring Boot 2.0+的自动化配置参数校验框架,自定义校验信息会覆盖默认信息,返回信息可设置中文或英文,可设置返回校验信息json字符串的key。

依赖添加

<dependency>
    <groupId>io.github.wilson-he</groupId>
    <artifactId>web-common-validation</artifactId>
    <version>LATEST</version>
</dependency>

快速开始

Application.java

import StringListVO;
import io.swagger.annotations.Api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

/**
 * Application
 *
 * @author Wilson
 * @since 2018/11/17
 */
@RestController
@RequestMapping("/")
@Api
@SpringBootApplication
@Validated
public class Application {
    public static void main(String[] args) {
	SpringApplication.run(Application.class);
    }

    @GetMapping("/index")
    public String index(@NotBlank @Pattern(regexp = "\\d+") @RequestParam String name){
	return name;
    }
    @GetMapping("/name")
    public String name(@NotBlank @Pattern(regexp = "\\d+",message = "正则错误") @RequestParam String name){
	return name;
    }

    @PostMapping("/vo")
    public String index(@Validated @RequestBody StringListVO vo){
	return "yes";
    }
}

application.yml

web:
  common:
    validation:
      msg-locale: zh_CN  #参数错误返回的信息语种
    #  code-key: code     #参数错误返回码的key字符串命名,默认"code"
    #  msg-key: error     #参数错误返回信息的key字符串命名,默认"msg"
swagger: #swagger自动化配置,需加入swagger2-spring-boot-starter依赖
  enabled: true
  docket:
    base-package: io.github.swagger.demo

效果展示

当同一参数多于1个不符合校验规则则会随机返回一个错误信息,即name参数为空时可能返回"name不能为空"或自定义信息"正则错误"。自定义的校验信息会覆盖原注解固定校验信息,如@NotBlank(message = "名字不能为空")则将返回"名字不能为空",无定义则会返回默认信息:参数名+错误信息(即"name不能为空") get参数校验 post vo校验

Versions

Version
1.0.2
1.0.1
1.0.0.RELEASE