SpringBoot 参数校验框架
spring-boot-starter-web
: web应用,引入这个starter后,可以不用再引入spring-boot-starter-web
spring-boot-starter-aop
: 主要用到了AOP,面向切面编程。aviator
: 谷歌的规则引擎,主要用来校验参数fastjson
: 阿里Json工具commons-lang3
: apache工具lombok
: 工具swagger2
: 生成接口文档
软件架构说明
- mvn clean install
- 引入依赖
<dependency>
<groupId>com.ler</groupId>
<artifactId>jcheck-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
见 南诏Blog JCheck参数校验框架之创建自己的SpringBoot-Starter
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ler</groupId>
<artifactId>checkdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>checkdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--自定义的starter-->
<dependency>
<groupId>com.ler</groupId>
<artifactId>jcheck-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring-boot-starter-test
这个打包进去的话,作用域不好弄,就不打包了
#环境
ler.con.env=gray
#swagger标题,中文会乱码
ler.swagger.title=JCheck Demo
#swagger版本
ler.swagger.version=1.0.0
#swagger要扫描的controller包
ler.swagger.controller-package-name=com.ler.checkdemo.controller
package com.ler.checkdemo.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author lww
* @date 2019-09-05 01:05
*/
@Data
@ApiModel(value = "测试对象", description = "测试")
public class User {
@ApiModelProperty("姓名")
private String name;
@ApiModelProperty("年龄")
private Integer age;
}
package com.ler.checkdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author lww
*/
@SpringBootApplication
public class CheckdemoApplication {
public static void main(String[] args) {
SpringApplication.run(CheckdemoApplication.class, args);
}
}
package com.ler.checkdemo.controller;
import com.ler.checkdemo.domain.User;
import com.ler.jcheckspringbootstarter.annation.Check;
import com.ler.jcheckspringbootstarter.config.HttpResult;
import com.ler.jcheckspringbootstarter.util.Checks;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @author lww
* @date 2019-09-03 17:26
*/
@Api("检查测试")
@RestController
public class ExampleController {
@ApiOperation("普通参数检查")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "姓名"),
@ApiImplicitParam(name = "age", value = "年龄"),
})
@GetMapping("/param")
@Check(ex = "name != null", msg = "name为空!")
@Check(ex = "age != null", msg = "age为空!")
public String input(String name, Integer age) {
Checks.isTrue(false, "Checks检查异常,类似Assert断言");
return "Success";
}
@ApiOperation("对象参数检查")
@PostMapping("/body")
@Check(ex = "user.name != null", msg = "name为空!")
@Check(ex = "user.age != null", msg = "age为空!")
public HttpResult b(@RequestBody User user, HttpServletRequest request) {
Assert.isTrue(false, "断言异常灰度不报!");
return HttpResult.success();
}
}
@Check(ex = "name != null", msg = "name为空!")
按照注解顺序依次校验
@Check(ex = "age != null", msg = "age为空!")
Checks.isTrue(false, "Checks检查异常,类似Assert断言");
@Check(ex = "user.name != null", msg = "name为空!")
@Check(ex = "user.age != null", msg = "age为空!")
Assert.isTrue(false, "断言异常灰度不报!");
因为配置的环境是灰度,所以不显示断言异常灰度不报!
starter项目源码 测试项目源码
- Fork 本仓库
- 新建 Feat_xxx 分支
- 提交代码
- 新建 Pull Request
- 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
- 码云官方博客 blog.gitee.com
- 你可以 https://gitee.com/explore 这个地址来了解码云上的优秀开源项目
- GVP 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目
- 码云官方提供的使用手册 https://gitee.com/help
- 码云封面人物是一档用来展示码云会员风采的栏目 https://gitee.com/gitee-stars/