Spring Boot @Validation @Valid

@Valid是使用hibernate validation的时候使用

@Validated 是只用spring  Validator 校验机制使用

基于方法参数的校验对基本类型的校验

 

例如:以下服务触发第一个参数的验证,确保其大小在8到10之间:

@Service
@Validated
public class MyBean {
   public Archive findByCodeAndAuthor(@Size(min = 8, max = 10) String code,
    Author author) {
    ...
   }
}

基于方法参数的校验对象校验


@ToString
@Data
@EqualsAndHashCode
public class UserDTO {
    private Integer id;
    /**
     * {@code @NotNull javax.validation 注意引入的包 }
     */
    @NotNull
    private String account;
    @NotNull
    private String name;
    @NotNull
    private String sex;
    @NotNull
    @Pattern(regexp="^[0-9]{11}$")
    private String phone;
    @NotNull
    @Email
    private String email;
}

 /**
     * 添加
     * 
     *@Validated必须添加否则只在对象字段上添加不会生效
     * @return ResponseEntity<Void>
     */
    @PostMapping
    public ResponseEntity<Void> save(@Validated  @RequestBody  UserDTO userDTO) {
        userService.insert(userDTO);
        return ResponseEntity.ok().build();
    }         

 

嵌套校验:

第一种

package org.niugang.coding.dto;

import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.*;

/**
* 数据传输对象
*
* @author Created by niugang on 2018/12/26/13:43
*/
@ToString
@Data
@EqualsAndHashCode
@ApiModel
public class UserDTO {

@ApiModelProperty(value = "ID", notes = "添加不同传id")
private Integer id;
/**
* {@code @NotNull javax.validation 的包 }
*/
@ApiModelProperty(value = "账号")
@NotNull
private String account;
@ApiModelProperty(value = "姓名")
@NotNull
private String name;
@NotNull
@ApiModelProperty(value = "性别")
private String sex;
@NotNull
@Pattern(regexp = "^[0-9]{11}$")
@ApiModelProperty(value = "电话")
private String phone;
@NotNull
@Email
@ApiModelProperty(value = "邮箱")
private String email;

@Valid //嵌套校验
public InnerUserDTo innerUserDTo= new InnerUserDTo();

@Data
private static class InnerUserDTo{
@NotEmpty
private String children;
}

}

 

第二种

package org.niugang.coding.dto;

import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.*;

/**
* 数据传输对象
*
* @author Created by niugang on 2018/12/26/13:43
*/
@ToString
@Data
@EqualsAndHashCode
@ApiModel
public class UserDTO {

@ApiModelProperty(value = "ID", notes = "添加不同传id")
private Integer id;
/**
* {@code @NotNull javax.validation 的包 }
*/
@ApiModelProperty(value = "账号")
@NotNull
private String account;
@ApiModelProperty(value = "姓名")
@NotNull
private String name;
@NotNull
@ApiModelProperty(value = "性别")
private String sex;
@NotNull
@Pattern(regexp = "^[0-9]{11}$")
@ApiModelProperty(value = "电话")
private String phone;
@NotNull
@Email
@ApiModelProperty(value = "邮箱")
private String email;

@Valid //嵌套校验
  @NotNull
public InnerUserDTo innerUserDTo;

@Data
private static class InnerUserDTo{
@NotEmpty
private String children;
}

}

                                                           

                        

微信公众号

                          
posted @ 2020-01-13 11:54  盲目的拾荒者  阅读(3255)  评论(0编辑  收藏  举报