在新增用户传参时进行参数校验,不符合校验规则会进行拦截报错
User实体bean
@Data
public class User{
@NotBlank(message = "图片地址不能为空")
@Length(min = 0,max = 1024,message = "图片地址长度超过限制")
@Pattern(regexp = "^[\\u4e00-\\u9fa5_a-zA-Z0-9]+$",message = "图片地址不能含有特殊字符")
private String imageUrl;//图片地址
@NotNull(message = "排序不能为空")
@Range(min = 1,max = 10,message = "排序长度超过限制")
private Integer sort;
@NotNull(message = "firstId不能为空")
@Min(value = 1,message = "firstId不能小于1")
private Long firstId;
@Pattern(regexp ="^[1][3,4,5,6,7,8,9][0-9]{9}$", message = "手机号格式有误")
private String mobile;
@Pattern(regexp = "^\\d{17,21}$",message = "会员ID只能17~21位数字")
private String memberCode;
@Pattern(regexp = "^(?!·)(?!.*?·$)[\u4E00-\u9FA5\uf900-\ufa2d|a-zA-Z·s]{1,10}$",message = "姓名只能1~10位的中英文")
private String userName;
}
controller层 方法中添加@Validated 注解
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public Response addUser(@RequestBody @Validated User user) {
//新增用户
}
其他一些注解