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中

相关推荐
十一082993几秒前
【PDF-XSS攻击】springboot项目-上传文件-解决PDF文件XSS攻击
spring boot·pdf·xss
风象南1 小时前
Spring Shell命令行工具开发实战
java·spring boot·后端
小小霸王龙!2 小时前
互联网大厂Java面试实录:Spring Boot与微服务在电商场景中的应用
java·spring boot·redis·微服务·电商
大只鹅3 小时前
Springboot3.3.4使用spring-data-elasticsearch整合Elasticsearch7.12.1
spring boot·elasticsearch
1.01^10004 小时前
[6-02-01].第05节:配置文件 - YAML配置文件语法
spring boot
知了一笑5 小时前
SpringBoot3集成多款主流大模型
spring boot·后端·openai
paopaokaka_luck6 小时前
基于SpringBoot+Vue的酒类仓储管理系统
数据库·vue.js·spring boot·后端·小程序
白仑色7 小时前
Spring Boot 性能优化与最佳实践
spring boot·后端·性能优化·数据库层优化·jvm 层优化·日志优化·transactional优化
风象南7 小时前
SpringBoot基于Java Agent的无侵入式监控实现
java·spring boot·后端
崎岖Qiu8 小时前
【Spring篇08】:理解自动装配,从spring.factories到.imports剖析
java·spring boot·后端·spring·面试·java-ee