admin管理员组

文章数量:1122847

邮箱

1:邮箱

@Pattern(regexp = ".+@.+\\.com$", message = "Email格式不正确")
private String email;

2:手机号

    @ApiModelProperty(value = "联系人电话")@NotBlank(message = "手机号码不能为空", groups = {Insert.class})@NotNull(message = "手机号不能为空", groups = {Insert.class})@Length(min = 11, max = 11, message = "手机号只能为11位")@Pattern(regexp = "^[1][3,4,5,6,7,8,9][0-9]{9}$", message = "手机号格式有误")private String phone;

3:在Controller 层添加校验方法

@RequestBody(required = true) @Validated User user

4:全局异常处理类

@RestControllerAdvice
@Slf4j
public class ExceptionControllerAdvice {@ExceptionHandler(value = MethodArgumentNotValidException.class)public Result handleValidException(MethodArgumentNotValidException e) {log.error("数据校验出现问题{},异常类型{}", e.getMessage(), e.getClass());BindingResult bindingResult = e.getBindingResult();HashMap<String, Object> errorMap = new HashMap<>();bindingResult.getFieldErrors().forEach(item->{System.out.println(item);errorMap.put(item.getField(), item.getDefaultMessage());});return Result.fail().message(String.valueOf(errorMap));}
}

结果:

本文标签: 邮箱