springboot3+vue3融合项目实战-大事件文章管理系统-更新文章分类和增加文章分类优化-分组校验

因为我们之前在category实体类里面增加了@notnull注解,而之前新增文章分类模块新增文章是不需要id的,而id是由数据库自动分配的,这就导致不能新增文章了,所以我们要进行分组校验

思路如下:

我们更改category代码:

c 复制代码
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;//更新时间

    //如果某个校验项目没有指定默认分组默认属于iDefault分组
    //分组之间可以继承,A extends B 那么B的所有属性都包含在A中
    public  interface Add extends Default {

    }

    public  interface Update extends Default{

    }

categoriycontroller中add方法修改

c 复制代码
@PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);


        return Result.success("添加文章成功");
    }

update方法修改

c 复制代码
@PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category){
        categoryService.update(category);
        return Result.success();
    }

注意的是
//如果某个校验项目没有指定默认分组默认属于iDefault分组
//分组之间可以继承,A extends B 那么B的所有属性都包含在A中

相关推荐
摇滚侠4 小时前
《SpringBoot 3:入门与应用实战》第 2 章 IOC 思想与实现 阅读笔记 1
spring boot·笔记·后端
ChaHae-In5 小时前
SpringBoot统一功能处理
java·spring boot·后端
今天的砖头有点烫手啊12 小时前
接口太慢?Spring Boot 缓存体系 @Cacheable 全链路拆解
spring boot·后端·缓存
zzzll111114 小时前
Spring Boot 入门指南:从零开始构建微服务
spring boot·后端·微服务
用户31268748772014 小时前
你的定时任务真的靠谱吗?Spring Boot @Scheduled 全链路拆解
spring boot
你住过的屋檐15 小时前
【Java】Argon2实现密码加密以及在springboot中使用教程
java·开发语言·spring boot·安全
程序员天天困15 小时前
Spring Boot i18n 国际化实战:从资源文件到多语言接口完整指南
spring boot·后端·编程语言
孫治AllenSun1 天前
【SpringBoot】计算机网络
spring boot·后端·计算机网络
Flittly1 天前
【雕虫大技】Agent 动态 Skill 供应链安全加固(二):执行隔离沙箱实战
java·spring boot·spring
代码什么用1 天前
Spring Task AND WebSocket
java·spring boot