springboot @Validated分组功能用在service层校验对象解决方案

最近做一个项目,用的springboot2.2.0.RELEASE+mybatisplus3.2.0架构,想着springboot这么强大了,里面有的功能都得用上秀一把技,结果翻车了。

在参数校验这块使用了spring-boot-starter-validation注解校验参数,可避免业务代码中无休止的参数校验判断,但在分层的方面来说,参数校验一般都是在Controller层完成的,但这一次情况特殊:提供的是dubbo服务。

话不多说,直接上代码

ValidGroup代码

public class ValidGroup {

    public interface Insert extends Default {
    }

    public interface Update extends Default {
    }


}

model代码

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("brand")
public class Brand extends BaseModel {

    @TableId(value = "id", type = IdType.AUTO)
    @NotNull(message = "{Brand.id.null}", groups = {ValidGroup.Update.class})
    private Long id;

    @NotBlank(message = "{Brand.brandCode.null}")
    @Size(min = 5, max = 5, message = "{Brand.brandCode.size}", groups = {ValidGroup.Insert.class, ValidGroup.Update.class})
    @TableField
    private String brandCode;

    @NotBlank(message = "{Brand.brandName.null}")
    @Size(min = 1, max = 50, message = "{Brand.brandName.size}", groups = {ValidGroup.Insert.class, ValidGroup.Update.class})
    private String brandName;

    @NotBlank(message = "{Brand.brandEnName.null}")
    @Size(min = 1, max = 50, message = "{Brand.brandEnName.size}", groups = {ValidGroup.Insert.class, ValidGroup.Update.class})
    private String brandEnName;

    @NotBlank(message = "{Brand.brandType.null}")
    @Size(min = 1, max = 50, message = "{Brand.brandType.size}", groups = {ValidGroup.Insert.class, ValidGroup.Update.class})
    private String brandType;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

}

service层代码

V1初始没分组时代码

@Validated
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Override
    public Brand getBrandByCode(String brandCode) {
        return getById(brandCode);
    }

    @Override
    public boolean saveBrand(@Valid Brand brand) {
        return save(brand);
    }

    @Override
    public boolean updateBrand(@Valid Brand brand) {
        return updateById(brand);
    }


}

V2有分组时代码

@Validated
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Override
    public Brand getBrandByCode(String brandCode) {
        return getById(brandCode);
    }

    @Override
    public boolean saveBrand(@Validated({ValidGroup.Insert.class}) Brand brand) {
        return save(brand);
    }

    @Override
    public boolean updateBrand(@Validated({ValidGroup.Update.class}) Brand brand) {
        return updateById(brand);
    }


}

V1版本是可以校验住的,只是在修改的方法里有些字段要排除掉,为了解决差异引入了分组,V2版本添加了分组后就直接提交给测试了。

过了没多久,来了一个bug,说创建和修改都失效了。。。

 

检查写法,百度,发现基本都是这样的写法

。。。。

检查了MethodValidationPostProcessor和MethodValidationInterceptor,一步步debug,没找到原因。。。

因有排期,所以就先用了if else搞定了校验。

 

正好今天他有空,之前排查打开的网页还没关,正好接着排查。

其中看到了

https://blog.csdn.net/blueheart20/article/details/88817754

https://reflectoring.io/bean-validation-with-spring-boot/

才发现写法有问题

后来换了写法搞定了

@Validated
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Override
    public Brand getBrandByCode(@NotBlank String brandCode) {
        return getById(brandCode);
    }

    @Override
    @Validated({ValidGroup.Insert.class})
    public boolean saveBrand(@Valid Brand brand) {
        return save(brand);
    }

    @Override
    @Validated({ValidGroup.Update.class})
    public boolean updateBrand(@Valid Brand brand) {
        return updateById(brand);
    }

}

 

 

 

上一篇:springboot条件装配以及原理


下一篇:推荐系统召回模型之YouTubeNet