spring validation多层对象校验教程
spring validation多层对象校验
1、第一层对象定义
import java.io.Serializable;
import javax.validation.Valid;
public class ReqIn<T> implements Serializable{
private static final long serialVersionUID = 25549320423002325L;
private String head;
@Valid
private T data;
public String getHead() {
return head;
}
public void setHead(String head) {
this.head = head;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
2、第二层对象
import java.io.Serializable;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
public class User implements Serializable{
private static final long serialVersionUID = 6747944028911495569L;
private String id;
@NotBlank
private String name;
@NotNull
private Integer age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
3、Controller层校验使用
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.spring.pro.model.ReqIn;
import com.spring.pro.model.User;
@RestController
public class UserController {
private Logger logger=LoggerFactory.getLogger(getClass());
@PostMapping("/valid")
public HttpStatus valid(@Valid @RequestBody ReqIn<User> reqIn){
logger.info("reqIn:{}",JSON.toJSONString(reqIn));
return HttpStatus.OK;
}
}
validation校验对象多个字段返回的消息内容顺序随机问题
问题描述
model中的代码如下
public class User {
@NotNull(message = "id不能为空", groups = UserGroup.UPDATE.class)
protected Integer id;
@NotBlank(message = "请输入姓名", groups = UserGroup.ADD.class)
private String name;
@NotBlank(message = "请输入性别", groups = UserGroup.ADD.class)
private String sex;
@NotBlank(message = "请输入手机号", groups = UserGroup.ADD.class)
private String phone;
@NotBlank(message = "请输入邮箱", groups = UserGroup.ADD.class)
private String email;
@NotBlank(message = "请输入公司名", groups = UserGroup.ADD.class)
private String companyName;
@NotBlank(message = "请输入职务", groups = UserGroup.ADD.class)
private String position;
}
定义的分组接口如下
public interface UserGroup {
interface ADD {
}
interface UPDATE {
}
}
在使用Spring的@Validated注解对对象中的多个参数进行校验时,在MethodArgumentNotValidException异常拦截器处发现如果有多个条件不符和校验规则的参数,MethodArgumentNotValidException中的bindingResult.getAllErrors()异常消息集合中的数据是按照随机顺序返回的,按照正常逻辑来说,我们肯定是想要按顺序将第一个不符合规范字段错误信息返回给用户。
解决办法
经过一番搜索发现可以给类上每个验证的注解都指定不同的group,然后在创建一个interface接口添加@GroupSequence对加在字段上的那些接口进行汇总,如下:
public interface UserGroup {
@GroupSequence({ADD.NAME.class,
ADD.SEX.class,
ADD.PHONE.class,
ADD.EMAIL.class,
ADD.COMPANY_NAME.class,
ADD.POSITION.class})
interface ADD {
interface NAME {}
interface SEX {}
interface PHONE {}
interface EMAIL {}
interface COMPANY_NAME {}
interface POSITION {}
}
interface UPDATE {
}
}
Model
@NoArgsConstructor
public class User {
@NotNull(message = "id不能为空", groups = UserGroup.UPDATE.class)
protected Integer id;
@NotBlank(message = "请输入姓名", groups = UserGroup.ADD.NAME.class)
private String name;
@NotBlank(message = "请输入性别", groups = UserGroup.ADD.SEX.class)
private String sex;
@NotBlank(message = "请输入手机号", groups = UserGroup.ADD.PHONE.class)
private String phone;
@NotBlank(message = "请输入邮箱", groups = UserGroup.ADD.EMAIL.class)
private String email;
@NotBlank(message = "请输入公司名", groups = UserGroup.ADD.COMPANY_NAME.class)
private String companyName;
@NotBlank(message = "请输入职务", groups = UserGroup.ADD.POSITION.class)
private String position;
}
在@GroupSequence注解中指定设置的接口数组顺序,然后在参数进行校验的时候直接在@Validated注解中放入UserGroup.ADD.class这个接口类即可public Result add(@Validated(UserGroup.ADD.class) @RequestBody User user),validation就会按照顺序返回错误信息,在异常拦截器中直接这样取出第一个错误信息即可MethodArgumentNotValidException.getBindingResult().getAllErrors().get(0).getDefaultMessage()
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341