java自定義注解驗證手機格式的實現(xiàn)示例
@Valid:Hibernate validation校驗機制
@Validated:Spring Validator校驗機制,這個也是最常用的
@Validation只是對@Valid進(jìn)行了二次封裝,在使用上并沒有太大區(qū)別,但在分組、注解位置、嵌套驗證等功能上有所不同
1.2 作用范圍@Validated:用在類型、方法和方法參數(shù)上。但不能用于成員屬性(field)@Valid:可以用在方法、構(gòu)造函數(shù)、方法參數(shù)和成員屬性(field)上
1.3 分組校驗@Validated:提供分組功能,可以在參數(shù)驗證時,根據(jù)不同的分組采用不同的驗證機制,注解中必須提供groups屬性,該屬性就是做分組的必要參數(shù)@Valid:沒有分組功能
2、未使用分組校驗的示例注解:
/** * 手機號驗證正則 */@Target({ElementType.FIELD,ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documented@Constraint(validatedBy = {PhoneValidator.class})// 指定約束處理器,也就是手機號格式驗證是哪個類來做校驗public @interface Phone { String pattern() default '^(?:(?:+|00)86)?1d{10}$'; String message() default '手機號格式非法'; Class<?>[] groups() default { }; // groups用來指定分組,可以讓校驗采取不同的機制,當(dāng)前默認(rèn)未指定任何分組機制,默認(rèn)每次都要進(jìn)行校驗 Class<? extends Payload>[] payload() default { }; // 默認(rèn)分組 interface Default{ } // 分組A interface A{ }}
格式校驗處理器:
/** * 校驗處理器:做手機號碼格式驗證的核心類 */public class PhoneValidator implements ConstraintValidator<Phone, String> { // 注解對象 private Phone phone; // 初始化【Phone】對象 @Override public void initialize(Phone constraintAnnotation) { phone = constraintAnnotation; } @Override public boolean isValid(String value, ConstraintValidatorContext context) { // 獲取【Phone】對象的手機格式驗證表達(dá)式 String pattern = phone.pattern(); Pattern compile = Pattern.compile(pattern); Matcher matcher = compile.matcher(value); return matcher.matches(); }
作用類:
@Data@EqualsAndHashCode(callSuper = false)@Accessors(chain = true)public class Person implements Serializable { @Phone private String phone;}
注意:只有在spring或者springboot項目中才能使用,直接調(diào)用方法不會有任何效果,使用注解進(jìn)行對象的屬性格式校驗時,必須配合@Validated一起使用(不一起使用,格式校驗注解將會無效),正確操作如下:
@RestController@RequestMapping('/admin/')public class PersonController { @Autowired private PersonService personService; @PostMapping('/query') public Person query(@RequestBody @Validated Person params) { return JsonResult.success(personService.queryByPhone(params)); }}
以上示例未使用分組功能,因此每次都會校驗。
3、分組校驗的示例使用分組校驗示示例時,先要看看@Validated注解,因為分組校驗就是配合該注解一起使用的,通過閱讀注釋就能理解到value屬性就是用來指定分組的:
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Validated { /** * Specify one or more validation groups to apply to the validation step * kicked off by this annotation. * <p>JSR-303 defines validation groups as custom annotations which an application declares * for the sole purpose of using them as type-safe group arguments, as implemented in * {@link org.springframework.validation.beanvalidation.SpringValidatorAdapter}. * <p>Other {@link org.springframework.validation.SmartValidator} implementations may * support class arguments in other ways as well. */ Class<?>[] value() default {};}
因此我們需要改動的位置有兩處:
首先是注解的作用類,注解上指定groups屬性 其次是controller中的請求的形參:在請求中形參的@Validated指定value值,也就是指定校驗生效的分組,如果請求中的分組類型【@Validated的value值】和作用類中注解所指定的分組【@Phone中的groups屬性的值】一致時,才會進(jìn)行校驗,否則不會執(zhí)行校驗作用類:
@Data@EqualsAndHashCode(callSuper = false)@Accessors(chain = true)public class Person implements Serializable { // 指定groups屬性 @Phone(groups = {Phone.A.class}) private String phone;}
controller層:
@RestController@RequestMapping('/admin/')public class PersonController { @Autowired private PersonService personService; @PostMapping('/query') public Person query(@RequestBody @Validated(Phone.A.class) Person params) { return JsonResult.success(personService.queryByPhone(params)); }}
此時請求中的校驗分組Phone.A.class和作用類中的校驗分組Phone.A.class一致,所以校驗會被執(zhí)行
到此這篇關(guān)于java自定義注解驗證手機格式的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)java自定義注解驗證手機格式內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
