把校验项进行归类分组,在完成不同的功能的时候,校验指定组中的校验项
定义分组
定义校验项时指定归属的分组
校验时指定要校验的分组
1,实体类添加注解:
- package com.jinhei.pojp;
-
- import com.fasterxml.jackson.annotation.JsonFormat;
- import jakarta.validation.constraints.NotEmpty;
- import jakarta.validation.constraints.NotNull;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import java.time.LocalDateTime;
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Category {
- @NotNull(groups = Update.class)
- private Integer id;//主键ID
- @NotEmpty(groups = {Add.class, Update.class})
- private String categoryName;//分类名称
- @NotEmpty(groups = {Add.class, Update.class})
- private String categoryAlias;//分类别名
- private Integer createUser;//创建人ID
- @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
- private LocalDateTime createTime;//创建时间
- @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
- private LocalDateTime updateTime;//更新时间
-
-
- public interface Add{
-
- }
- public interface Update{
-
- }
- }
-
复制代码 2,使用代码演示:
- package com.jinhei.controller;
-
- import com.jinhei.pojp.Category;
- import com.jinhei.pojp.Result;
- import com.jinhei.service.CategoryService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.List;
-
- @RestController
- @RequestMapping("/category")
- public class CategoryController {
- @Autowired
- private CategoryService categoryService;
- /**
- * 添加分类
- */
- @PostMapping("/add")
- public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
- categoryService.add(category);
- return Result.success();
- }
- /**
- * 分类列表
- */
- @GetMapping
- public Result<List<Category>> list(){
- List<Category> cs = categoryService.list();
- return Result.success(cs);
- }
- /**
- * 分类详情
- */
- @GetMapping("/detail")
- public Result<Category> detail(Integer id){
- Category c = categoryService.findById(id);
- return Result.success(c);
- }
- /**
- * 修改分类
- */
- @PutMapping
- public Result update(@RequestBody @Validated(Category.Update.class) Category category){
- categoryService.update(category);
- return Result.success();
- }
- }
复制代码 优化,使用默认分组:
//如果说某个校验项没有指定分组,默认属于Default分组
//分组之间可以继承, A extends B 那么A中拥有B中所有的校验项
- package com.jinhei.pojp;
-
- import com.fasterxml.jackson.annotation.JsonFormat;
- import jakarta.validation.constraints.NotEmpty;
- import jakarta.validation.constraints.NotNull;
- import jakarta.validation.groups.Default;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import java.time.LocalDateTime;
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Category {
- @NotNull(groups = Update.class)
- private Integer id;//主键ID
- @NotEmpty
- private String categoryName;//分类名称
- @NotEmpty
- private String categoryAlias;//分类别名
- private Integer createUser;//创建人ID
- @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
- private LocalDateTime createTime;//创建时间
- @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
- private LocalDateTime updateTime;//更新时间
-
-
- public interface Add extends Default {
-
- }
- public interface Update extends Default{
-
- }
- }
-
复制代码
|