参数校验: spring-boot-starter-validation
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
应用
@PostMapping("/login")
public Result login(@Pattern(regexp = "^\\S{5,15}$") String username,
@Pattern(regexp = "^\\S{5,15}$") String password) {
...........
}
@PutMapping("/update")
public Result update(@RequestBody @Validated User user) {
userService.update(user);
return Result.suc();
}
实体类
@Data
public class User {
@NotNull
@TableId
Integer id;
String username;
@JsonIgnore
String password;
@NotEmpty
@Pattern(regexp="^\\S{1,10}$")
String nickname;
@NotEmpty
@Email
String email;
String userPic;
LocalDateTime createTime;
LocalDateTime updateTime;
}
# 分组校验
@Data
public class Category {
@NotNull(groups = Update.class)
@TableId(type = IdType.AUTO)
Integer id;
@NotEmpty
String categoryName;
@NotNull
String categoryAlias;
Integer createUser;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime updateTime;
public interface Add extends Default {
}
public interface Update extends Default {
}
}
文章来源地址https://www.toymoban.com/news/detail-810896.html
文章来源:https://www.toymoban.com/news/detail-810896.html
到了这里,关于参数校验: spring-boot-starter-validation的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!