hibernate validator
我们经常需要对参数进行校验,很多情况下都是一个个参数校验,这种校验方式虽然也达到了效果,但是看起来就比较别扭。而且如果接收参数为map
的时候,不太好处理,我在网上找资料也没有找到符合我情况的资料,于是去hibernate
官网翻了下文档,在此分享下思路。
-
首先编写实体类,添加注解
-
调用
hiberante-validator
校验参数,校验不通过的会放在Set
里面 -
遍历
Set
的值,有值则抛出异常 -
统一异常处理,针对异常信息处理
具体实现
/**
* @Author: code-generate
* @Date: 2020/09/14 14:15:51
* @Description:
*/
@Data
public class RoleInfo implements Serializable {
private static final long serialVersionUID = 1L;
/** */
@TableId(type = IdType.AUTO)
private Long id;
/** 角色名 */
@NotNull(message = "角色名称不能为空")
private String roleName;
/** 角色编号 */
@NotNull(message = "角色编号不能为空")
private String roleCode;
/** 系统编号 */
@NotNull(message = "系统编号不能为空")
private String sysCode;
}
校验工具类
/**
* hibernate validator 参数校验
*
* @author Hou_fx
* @date 2020.9.14 17:53
* @description
*/
public class ValidatorUtil {
private static Validator validator;
private static Validator init() {
if (validator == null) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}
return validator;
}
public static <T> void validator(T checkObject) {
init();
Set<ConstraintViolation<T>> res = validator.validate(checkObject);
res.forEach(f -> {
throw new BizException(ResponseStatus.PARAMS_INVALIDE.getCode(), f.getMessage());
});
}
}
异常处理
public class BizException extends RuntimeException {
private String code;
public BizException(String code, String message) {
super(message);
this.code = code;
}
public BizException(ResponseStatus responseStatus) {
super(responseStatus.getMessage());
this.code = responseStatus.getCode();
}
}
全局异常处理
@ControllerAdvice
public class ExceptionsHandler {
private static final Logger LOG = LoggerFactory.getLogger(ExceptionsHandler.class);
@ExceptionHandler(BizException.class)
@ResponseBody
public ResultData exceptionCheckData(HttpServletRequest request, BizException ex) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ResultData result = new ResultData();
result.setStatus(ex.getCode());
result.setMessage(ex.getMessage());
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setFeatures(Feature.OrderedField);
BodyReaderHttpServletRequestWrapper httpReq = (BodyReaderHttpServletRequestWrapper) request;
Request requestObj = JSON.parseObject(httpReq.getBody(), Request.class, fastJsonConfig.getFeatures());
stopWatch.stop();
commonLogService.saveCommonLog(requestObj,result,request.getRequestURI(),stopWatch.getTotalTimeSeconds(),ex);
return result;
}
}
使用方式也非常简单: ValidatorUtil.validator(roleInfo);
就可以了,有异常会直接返回信息,也不用再写多余的校验了。
总结
这种方式与大部分资料不同的是,现在大部分的处理方式都是在 controller
调用的地方使用@Valid
注解,然后使用BindingResult
操作,通过全局异常处理,这种方式不需要使用@Valid
注解和BindingResult
,相同点是,也是通过全局异常处理,可随地使用,如果是Map
的话,先转为java bean
再对java bean
操作即可
最后贴一下参考文档:https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-validating-bean-constraints