- 前言
- 一、业务场景?
- 二、实现步骤
- 1.引入依赖
- 2.新增自定义注解(注解嵌套)
- 3. 自定义注解类处理器
- 4. 自定义异常类
- 5. 全局异常捕获类
- 6. 实体类上的注解
- 7. Controller层
- 结果显示
- 三、踩坑日记
- 四、参考博客
前言
一、业务场景?
对于一些数据库表字段是唯一索引的,需要在数据插入前做数据重复性检验,并友好返回给前端。例如用户名不能重复,工号不能重复等。对于这些重复判断项目中很多地方都需要用到,想法是直接抽离处理,统一管理,减少重复代码。按照以下步骤配置,字段值重复检验一个注解即可实现。以下是实现步骤,如有疑问,欢迎大家提问和指点。
二、实现步骤 1.引入依赖代码如下(示例):
2.新增自定义注解(注解嵌套)org.springframework.boot spring-boot-starter-validation 2.3.11.RELEASE
@FieldRepeatValidators
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FieldRepeatValidatorHandler.class)
public @interface FieldRepeatValidators {
String tableName();
String idField() default "id";
String idProperty() default "id";
FieldRepeatValidator[] fieldRepeatValidators();
String message() default "";
Class>[] groups() default { };
Class extends Payload>[] payload() default { };
}
@FieldRepeatValidator
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.TYPE})
public @interface FieldRepeatValidator {
String field();
String message() default "字段内容重复!";
String property();
}
3. 自定义注解类处理器
public class FieldRepeatValidatorHandler implements ConstraintValidator4. 自定义异常类, ApplicationContextAware { private FieldRepeatValidators fieldRepeatValidators; static ApplicationContext application; @Override @SuppressWarnings("all") public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { application = applicationContext; } @Override public void initialize(FieldRepeatValidators fieldRepeatValidators) { this.fieldRepeatValidators = fieldRepeatValidators; } @Override public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) { return fieldRepeat(o,fieldRepeatValidators); } public static boolean fieldRepeat(Object object, FieldRepeatValidators fieldRepeatValidators) { JSONObject jsonObject = JSONUtil.parseObj(object); String table = fieldRepeatValidators.tableName(); boolean isUpdate; String idField = fieldRepeatValidators.idField(); String idProperty = fieldRepeatValidators.idProperty(); isUpdate = jsonObject.containsKey(idProperty); Object id = jsonObject.get(idProperty); HashMap errors = new HashMap<>(); for(FieldRepeatValidator fieldRepeatValidator : fieldRepeatValidators.fieldRepeatValidators()){ String field = fieldRepeatValidator.field(); String property = fieldRepeatValidator.property(); String message = fieldRepeatValidator.message(); Object o = jsonObject.get(property); if(Objects.isNull(o)){ continue; } String sql = StrUtil.format("select count(1) from {} where {} = '{}'",table,field,o); if(isUpdate){ sql = StrUtil.format(sql + " and {} = {}",idField,id); } JdbcTemplate jdbcTemplate = application.getBean("jdbcTemplate", JdbcTemplate.class); Map map = jdbcTemplate.queryForMap(sql); if(!map.get("count").equals(0L)){ errors.put(property,message); } } if(errors.size() != 0){ throw new FieldRepeatException(errors); } return true; } }
@Data
public class FieldRepeatException extends ConstraintDeclarationException {
private HashMap errors;
public FieldRepeatException(HashMap errors) {
this.errors = errors;
}
}
5. 全局异常捕获类
@RestControllerAdvice()
@Slf4j
public class GlobalControllerExceptionHandler {
@ExceptionHandler(FieldRepeatException.class)
private R fieldRepeatExceptionHandler(FieldRepeatException e) {
return R.error("40001","数据检验失败").put("error",e.getErrors()));
}
}
6. 实体类上的注解
@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "User对象", description = "用户表")
@TableName("user")
@FieldRepeatValidators(tableName = "user",idField = "id",fieldRepeatValidators = {
@FieldRepeatValidator(field = "user_name", property = "userName",message = "用户名重复!"),
@FieldRepeatValidator(field = "mobile", property = "mobile",message = "电话号码重复!")
})
public class User extends Model {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "用户名")
private String userName;
@ApiModelProperty(value = "电话")
private String mobile;
}
7. Controller层
@PostMapping("/save")
@ApiOperation(value = "新增", notes = "传入user")
public R save(@RequestBody @Validated User user) {
// 增加@Validated注解
return R.data(userService.save(user));
}
结果显示
{
"code":40001,
"message":"数据检验失败",
"errors":{
"userName":"用户名重复!",
"mobile":"电话号码重复!"
}
}
三、踩坑日记
问题:
检验完成,抛出自定义异常会出现: unexpected exception during isValid call
原因:
自定义异常由于继承的是RunTimeException,导致在检验处理类执行isValid()方法时没有正常返回值,而抛出了javax.validation.ValidationException。导致自定义异常无法被全局异常处理类捕获。
解决方法:
自定义异常需要继承ConstraintDeclarationException
public class FieldRepeatException extends ConstraintDeclarationException{}
问题:
javax.validation.ConstraintDefinitionException: HV000074:containsbut does not contain a message parameter
原因:
自定义检验字段注解缺少message参数
解决方法:
添加message参数等
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FieldRepeatValidatorHandler.class)
public @interface FieldRepeatValidators {
String message() default "";
Class>[] groups() default { };
Class extends Payload>[] payload() default { };
}
问题:
自定义检验字段注解添加在字段上,只能拿到该字段的当前值。这样的话就不能获取到其他字段的值,其中最重要的是id字段的值。id字段值的作用是判断此次请求是新增还是修改,如果是新增,则只需要判断数据库中是否含有该字段值的记录。如果是修改,则需要判断除了自身id记录外是否还含有该字段值的记录。
解决方法:
自定义检验字段注解在类上,则可以获取到全部字段的值。再通过注解逐一对有需要做重复检验的字段进行检验,返回检验结果。
@FieldRepeatValidators(tableName = "user",idField = "id",fieldRepeatValidators = {
@FieldRepeatValidator(field = "user_name", property = "userName",message = "用户名重复!"),
@FieldRepeatValidator(field = "mobile", property = "mobile",message = "电话号码重复!")
})
public class User extends Model {
}
四、参考博客
https://blog.csdn.net/weixin_44912855/article/details/122720171
https://juejin.cn/post/6844903950768930830#heading-14



