码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot @Validation @Valid

时间:2020-01-13 12:39:12      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:不同   static   成长   equal   专注   idt   date   use   div   

@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;
}

}

                                                           

                        

                                                 微信公众号

                                               技术图片技术图片?

                                                                             JAVA程序猿成长之路

                          分享资源,记录程序猿成长点滴。专注于Java,Spring,SpringBoot,SpringCloud,分布式,微服务。 

Spring Boot @Validation @Valid

标签:不同   static   成长   equal   专注   idt   date   use   div   

原文地址:https://www.cnblogs.com/niugang0920/p/12186638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!