文章目录 pom.xml ValidatorUtil.java IsMobileValidator.java IsMobile.java LoginVo.java LoginController.java
pom.xml
< dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-validation</ artifactId> < version> 2.4.5</ version> </ dependency>
ValidatorUtil.java
package com. example. util ; import org. junit. jupiter. api. Test ;
import org. springframework. util. StringUtils ; import java. util. regex. Matcher ;
import java. util. regex. Pattern ;
public class ValidatorUtil { private static final Pattern mobile_pattern = Pattern . compile ( "^[1][3-9][0-9]{9}$" ) ; public static boolean isMobile ( String mobile) { if ( ! StringUtils . hasText ( mobile) ) { return false ; } Matcher matcher = mobile_pattern. matcher ( mobile) ; return matcher. matches ( ) ; } @Test public void t1 ( ) { String mobile = "0133333333" ; System . out. println ( isMobile ( mobile) ) ; String mobile2 = "13333333333" ; System . out. println ( isMobile ( mobile2) ) ; } }
IsMobileValidator.java
package com. example. validator ; import org. springframework. util. StringUtils ;
import com. example. util. ValidatorUtil ; import javax. validation. ConstraintValidator ;
import javax. validation. ConstraintValidatorContext ;
public class IsMobileValidator implements ConstraintValidator < IsMobile , String > { private boolean required = false ; @Override public void initialize ( IsMobile constraintAnnotation) { required = constraintAnnotation. required ( ) ; } @Override public boolean isValid ( String value, ConstraintValidatorContext context) { if ( required) { return ValidatorUtil . isMobile ( value) ; } else { if ( ! StringUtils . hasText ( value) ) { return true ; } else { return ValidatorUtil . isMobile ( value) ; } } }
}
IsMobile.java
package com. example. validator ; import javax. validation. Constraint ;
import javax. validation. Payload ;
import java. lang. annotation. Documented ;
import java. lang. annotation. Retention ;
import java. lang. annotation. Target ; import static java. lang. annotation. ElementType . * ;
import static java. lang. annotation. RetentionPolicy . RUNTIME ;
@Target ( { METHOD , FIELD , ANNOTATION_TYPE , CONSTRUCTOR , PARAMETER , TYPE_USE } )
@Retention ( RUNTIME )
@Documented
@Constraint ( validatedBy = { IsMobileValidator . class } )
public @interface IsMobile { String message ( ) default "手机号码格式错误" ; boolean required ( ) default true ; Class < ? > [ ] groups ( ) default { } ; Class < ? extends Payload > [ ] payload ( ) default { } ;
}
LoginVo.java
package com. example. vo ; import com. example. validator. IsMobile ;
import lombok. Data ;
import org. hibernate. validator. constraints. Length ; import javax. validation. constraints. NotNull ;
@Data
public class LoginVo { @NotNull @IsMobile private String mobile; @NotNull @Length ( min = 32 ) private String password; }
LoginController.java
package com. example. controller ; import com. example. service. UserService ;
import com. example. vo. LoginVo ;
import com. example. vo. RespBean ;
import lombok. extern. slf4j. Slf4j ;
import org. springframework. stereotype. Controller ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. ResponseBody ; import javax. annotation. Resource ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import javax. validation. Valid ; @Controller
@RequestMapping ( "/login" )
@Slf4j
public class LoginController { @Resource private UserService userService; @RequestMapping ( "/toLogin" ) public String toLogin ( ) { return "login" ; } @RequestMapping ( "/doLogin" ) @ResponseBody public RespBean doLogin ( @Valid LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) { System . out. println ( "controller层:" + loginVo) ; return userService. doLogin ( loginVo, request, response) ; } }