还在为冗长的参数校验而写if else吗?还在用hibernae.validator单一场景的参数校验器吗?那你就out了,java最新注解参数校验器,一次注解,到处使用,引入timing的概念,可以随时随地随心所欲地使用参数校验,方便快捷,高效省时,安全可靠,再也不用写简单而又复杂的参数校验啦!
github地址(帮忙star):https://github.com/Anyzm/parameter-check
maven坐标:
com.github.anyzm parameter-starter 0.0.1-RELEASE
简单示例:
##1、spring project demo##
```
package com.example.demo;import cn.anyzm.parameter.annotation.AssertBoolean;import cn.anyzm.parameter.annotation.NotNull;import cn.anyzm.parameter.annotation.Range;import lombok.Data;/** * @author huangzhaolai-jk * @version 1.0.0 * @Description Student is used for * @Date 2020/07/12 - 18:43 */@Datapublic class Student { @NotNull(timing = "123") private String name; @Range(minValue = 1,maxValue = 18,msg = "年龄必须是1-18岁",timing = "456") private int age = 0; @AssertBoolean(timing = "789",value = false) private boolean flag = false;} public static void main(String[] args) throws Exception { Student student = new Student(); student.setAge(0); student.setFlag(true); ParameterCheckHandler.checkFiled(student); ParameterCheckHandler.checkFiled(student,"789"); }
```
ParameterCheckHandler.checkFiled has overload method,use timing by yourself,verify all the timing without timing input。
##2、spring-boot project demo##
```
package com.example.demo;import cn.anyzm.parameter.annotation.Verify;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @Author ZhaoLai Huang * created by ZhaoLai Huang on 2020/7/25 */@RestControllerpublic class TestController { @RequestMapping("/test") @Verify public String test(@Verify @RequestBody Student student) { System.out.println(student); return "ok"; }}
```
add @Verify on your method,and add @Verify before your parameter,you can use it with timing。if the parameter is the Basic types or String,you can directly use @NotNull,@NotEmpty and so on。
2020/08/05
参数校验一直是项目中一块简单而又繁琐的工作,parameter-check转为java项目准备,将你从逻辑简单实现繁琐的参数校验中解脱出来
——那月真美