mars-validated基于 spring 的參數校驗框架
mars-validated springmvc springboot springcloud dubbo 參數校驗
簡單好用的 springmvc springboot springcloud dubbo 參數校驗 此框架基于 spring 開發(fā)。
使用4步配置
1、導入jar
<dependency>
<groupId>com.github.fashionbrot</groupId>
<artifactId>mars-validated</artifactId>
<version>1.0.0</version>
</dependency>
2、使用注解
2.1 springboot 配置
fileName 如果不填默認jar 包自帶提示,如果需要批量自定義請按照jar 包下的valid_zh_CN.properties 修改提示語內容
@SpringBootApplication
@EnableValidatedConfig(fileName = "test") // fileName 默認中文jar包自帶 如需要批量自定義請自己創(chuàng)建 test.properties 放在自己項目中的resources 下
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2.2 spring配置
@Component
@Configuration
@EnableValidatedConfig(fileName = "valid_zh_CN")
public class Config {
}
3使用 @Validated 開啟接口驗證 @Email驗證郵箱格式
@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){
}
}
4 自定義實現全局異常處理
攔截 ValidatedException
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ValidatedException.class)
@ResponseStatus(HttpStatus.OK)
public RespMessage ValidationException(ValidatedException e){
return new RespMessage(-100,e.getMsg());
}
}
支持 默認值設置 hibernate默認不支持
import com.github.fashionbrot.validated.annotation.Default;
@Data
public class BaseModel {
@Default("1")
private Integer pageNo;
@Default("20")
private Integer pageSize;
}
支持 dubbo 接口、實現類上方法上添加 @Validated ,設置 dubbo DubboProviderFilter 攔截器做統(tǒng)一處理
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;
}
}
validated 參數驗證
使用環(huán)境
spring4.0 及以上
jdk1.8 及以上
| Annotation | Supported data types | 作用 |
|---|---|---|
| NotBlank | String | 驗證String 字符串是否為空 |
| NotNull | String,Object,Integer,Long,Double,Short,Float,BigDecimal, BigInteger | 驗證對象是否為空 |
| NotEmpty | String | 驗證字符串不能為空 |
| AssertFalse | Boolean,boolean,String | 只能為false |
| AssertTrue | Boolean,boolean,String | 只能為true |
| BankCard | String | 驗證銀行卡 |
| CreditCard | String | 驗證信用卡 |
| Default | Integer,Double,Long,Short,Float,BigDecimal,String | 設置默認值 |
| Digits | String | 驗證是否是數字 |
| String | 驗證是否是郵箱 | |
| IdCard | String | 驗證是否是身份證,驗證18歲 |
| Length | int,long,short,double,Integer,Long,Float,Double,Short,String | 驗證長度 |
| Pattern | String | 正則表達式驗證 |
| Phone | String | 驗證手機號是否正確 |
| Size | int,long,short,Integer,Long,Short | 驗證大小值 |
| NotEqualSize | String | 驗證長度 |
### 支持自定義注解 如下:
```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;
}
}
6、可通過 test項目中的 https://github.com/fashionbrot/mars-validated/tree/master/test/springboot-test 參考使用demo
7、如有問題請通過 https://github.com/fashionbrot/mars-validated/issues 提出 告訴我們。我們非常認真地對待錯誤和缺陷,在產品面前沒有不重要的問題。不過在創(chuàng)建錯誤報告之前,請檢查是否存在報告相同問題的issues。
評論
圖片
表情
