简单好用的 springmvc springboot springcloud dubbo 参数校验 此框架基于 spring 开发。
<dependency> <groupId>com.github.fashionbrot</groupId> <artifactId>mars-validated</artifactId> <version>1.0.0</version> </dependency>
fileName 如果不填默认jar 包自带提示,如果需要批量自定义请按照jar 包下的valid_zh_CN.properties 修改提示语内容
@SpringBootApplication @EnableValidatedConfig(fileName = "test") // fileName 默认中文jar包自带 如需要批量自定义请自己创建 test.properties 放在自己项目中的resources 下 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
@Component @Configuration @EnableValidatedConfig(fileName = "valid_zh_CN") public class Config { }
@Controller public class DemoController { @Autowired private TestService testService; @RequestMapping("/emailCheck") @ResponseBody @Validated //注意此处 public String demo(@Email String abc,){ return testService.test(abc); } } @Controller public class DemoController { @Autowired private TestService testService; @RequestMapping("/idcardCheck") @ResponseBody @Validated public String demo(IdCardModel idCardModel){ return testService.test("ac"); } @RequestMapping("/idcardCheck2") @ResponseBody public String demo2(IdCardModel idCardModel){ return testService.test2("ac"); } } **此处支持多继承验证*** public class IdCardModel extends BaseModel{ @IdCard private String idCardModel; public String getIdCardModel() { return idCardModel; } public void setIdCardModel(String idCardModel) { this.idCardModel = idCardModel; } } @Service public class TestService{ @Validated public void test2(@IdCard String abc){ } }
拦截 ValidatedException
@RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(ValidatedException.class) @ResponseStatus(HttpStatus.OK) public RespMessage ValidationException(ValidatedException e){ return new RespMessage(-100,e.getMsg()); } }
import com.github.fashionbrot.validated.annotation.Default; @Data public class BaseModel { @Default("1") private Integer pageNo; @Default("20") private Integer pageSize; }
public class DubboProviderFilter implements Filter { @Override public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { Result result = invoker.invoke(invocation); if(result.hasException() && result.getException() instanceof ValidationException){ throw new CustomException(result.getException()); //自定义异常,全局拦截控制,或抛出 RpcException 自行拦截 } return result; } }
spring4.0 及以上 jdk1.8 及以上
### 支持自定义注解 如下: ```bash 1、实现 ConstraintValidator 此接口 2、自定义注解如下: CustomConstraintValidator.class,CustomConstraintValidator2.class 实现类可多个,至少有一个 @Documented @Target({ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = {CustomConstraintValidator.class,CustomConstraintValidator2.class}) public @interface Custom { String msg() default "com.spv.valid.Custom.msg"; int min(); } 3、代码实现 public class CustomConstraintValidator implements ConstraintValidator<Custom,String> { @Override public boolean isValid(Custom custom,String var1) { /** * 自定义方法 */ int min=custom.min(); /** * value */ System.out.println(var1); /** * return true 则验证成功 false 验证失败 */ return true; } }
validated/tree/master/test/springboot-test> 参考使用demo
告诉我们。我们非常认真地对待错误和缺陷,在产品面前没有不重要的问题。不过在创建错误报告之前,请检查是否存在报告相同问题的issues。