后台数据校验优化

This commit is contained in:
xiongxiaoyang
2020-12-22 23:12:22 +08:00
parent 612555dbe6
commit 1046a7ffc1
8 changed files with 78 additions and 62 deletions

View File

@ -1,21 +1,30 @@
package com.java2nb.novel.core.advice;
import com.java2nb.novel.core.bean.ResultBean;
import com.java2nb.novel.core.enums.ResponseStatus;
import com.java2nb.novel.core.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 通用的异常处理器
*
* @author 11797*/
@Slf4j
@ControllerAdvice
@ResponseBody
@RestControllerAdvice
public class CommonExceptionHandler {
/**
* 处理后台数据校验异常
* */
@ExceptionHandler(BindException.class)
public ResultBean handlerBindException(BindException e){
log.error(e.getMessage(),e);
return ResultBean.fail(ResponseStatus.PARAM_ERROR);
}
/**
* 处理业务异常
* */

View File

@ -0,0 +1,11 @@
package com.java2nb.novel.core.valid;
/**
* 新增数据的校验分组
* @author xiongxiaoyang
*/
public interface AddGroup {
}

View File

@ -0,0 +1,11 @@
package com.java2nb.novel.core.valid;
/**
* 更新数据的校验分组
* @author xiongxiaoyang
*/
public interface UpdateGroup {
}

View File

@ -1,36 +1,56 @@
package com.java2nb.novel.entity;
import com.java2nb.novel.core.valid.AddGroup;
import com.java2nb.novel.core.valid.UpdateGroup;
import java.util.Date;
import javax.annotation.Generated;
import javax.validation.constraints.*;
public class User {
@Null(groups = {AddGroup.class, UpdateGroup.class})
@Generated("org.mybatis.generator.api.MyBatisGenerator")
private Long id;
@NotBlank(groups = {AddGroup.class},message="手机号不能为空!")
@Pattern(groups = {AddGroup.class},regexp="^1[3|4|5|6|7|8|9][0-9]{9}$",message="手机号格式不正确!")
@Generated("org.mybatis.generator.api.MyBatisGenerator")
private String username;
@NotBlank(groups = {AddGroup.class},message="密码不能为空!")
@Null(groups = {UpdateGroup.class})
@Generated("org.mybatis.generator.api.MyBatisGenerator")
private String password;
@Null(groups = {AddGroup.class})
@Generated("org.mybatis.generator.api.MyBatisGenerator")
private String nickName;
@Null(groups = {AddGroup.class})
@Generated("org.mybatis.generator.api.MyBatisGenerator")
private String userPhoto;
@Null(groups = {AddGroup.class})
@Min(value = 0,groups = {UpdateGroup.class})
@Max(value = 1,groups = {UpdateGroup.class})
@Generated("org.mybatis.generator.api.MyBatisGenerator")
private Byte userSex;
@Null(groups = {AddGroup.class,UpdateGroup.class})
@Generated("org.mybatis.generator.api.MyBatisGenerator")
private Long accountBalance;
@Null(groups = {AddGroup.class,UpdateGroup.class})
@Generated("org.mybatis.generator.api.MyBatisGenerator")
private Byte status;
@Null(groups = {AddGroup.class,UpdateGroup.class})
@Generated("org.mybatis.generator.api.MyBatisGenerator")
private Date createTime;
@Null(groups = {AddGroup.class,UpdateGroup.class})
@Generated("org.mybatis.generator.api.MyBatisGenerator")
private Date updateTime;