import javax.validation.ConstraintViolation; import org.springframework.stereotype.Service; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import com.dragon.common.BusinessException; import com.dragon.common.enums.BizExceptionEnum; @Service public class MyValidatorBean extends LocalValidatorFactoryBean{ public <T> void validates(T object, Class<?>... groups) { Set<ConstraintViolation<T>> result = super.validate(object, groups); StringBuilder sb = new StringBuilder(); for (ConstraintViolation<T> constraintViolation : result) { sb.append(constraintViolation.getPropertyPath()+constraintViolation.getMessage()); } if(sb.length()>1) { throw new BusinessException(BizExceptionEnum.PARAM_ERROR, sb.toString()); } } }
支持hibernate.validator下的参数校验注解,
使用时,注入,直接调用方法即可,这里抛出了我自定义的业务异常然后AOP统一处理
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步