springboot注解实体类校验参数
作者:互联网
spring boot校验参数
导入依赖
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
接口中添加注解@Valid
@PostMapping("add")
@ResponseBody
public Result add(@RequestBody @Valid UserAddRequest userAddRequest) {
return ResultEnum.SUCCESS.toRet();
}
实体类注解
import lombok.Data;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
@Data
public class UserAddRequest {
@NotBlank(message = "用户名不能为空")
private String username;
@Max(value = 3, message = "年龄不合法")
@Min(value = 1, message = "年龄不合法")
private int sex;
}
标签:实体类,springboot,校验,注解,import,message,validation,javax,constraints 来源: https://blog.csdn.net/weixin_44800367/article/details/122158982