标签:不同 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) {
...
}
}
/**
* 添加
*
*@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