Springboot集成BeanValidation扩展一:错误提示信息加公共模板
Bean Validator扩展
1、需求
在使用validator时,有个需求就是公用错误提示信息,什么意思?
举个例子:
@NotEmpty非空判断,在资源文件中我不想每个非空判断都写”不能为空“,只需要写”###“,然后提示信息自动会变成”###不能为空“
代码:
public class User{
//资源文件中user.name.empty=用户名
@NotEmpty(key={user.name.empty})
private String name;
'''
}
//加入name为空,则最终的错误提示为“用户名不能为空”(会自动加上“不能为空”信息)
2、实现方式
有两种实现方式
方式一:手动调用验证方法
注解
@Target({FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@ReportAsSingleViolation
@Constraint(validatedBy = {})
@NotNull
@Size(min = 1)
public @interface NotEmpty {
String message() default "{key}{com.chyjr.hyb.validator.constraints.empty.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
String key() default "";
}
验证器
//验证器
public class MyValidator {
private static final Logger log = LoggerFactory.getLogger(HybValidator.class);
private static Validator validator = null;
private static MessageInterpolator msgInterpolator = null;
static {
if (validator == null) {
LocalValidatorFactoryBean factory =
(LocalValidatorFactoryBean) ApplicationContextUtil.getBean("validator");
validator = factory.getValidator();
msgInterpolator = factory.getMessageInterpolator();
}
}
public static HybValidatorResult validate(Object object, Class<?>... groups) {
HybValidatorResult result = new HybValidatorResult();
Set<ConstraintViolation<Object>> violations = validator.validate(object, groups);
Map<String, String> map = new HashMap<>();
if (CollectionUtils.isEmpty(violations)) {
result.setErrors(false);
} else {
result.setErrors(true);
for (ConstraintViolation<Object> violation : violations) {
String path = violation.getPropertyPath().toString();
String message = violation.getMessage();
if (StringUtils.isBlank(path) || StringUtils.isBlank(message) || map.containsKey(path))
continue;
message = resolveMessage(message);
map.put(path, message);
}
result.setItems(map);
}
return result;
}
private static final Pattern elpattern = Pattern.compile("\\{[^{}]+\\}");
private static String resolveMessage(String message) {
Matcher matcher = elpattern.matcher(message);
try {
while (matcher.find()) {
String el = matcher.group();
//用资源文件信息替换message = {key}{my.empty.message}
//注解这里的key会替换成注解NotEmpty定义的key,即
//message = {user.name.empty}{my.empty.message}
String val = msgInterpolator.interpolate(el, null);
if (StringUtils.isBlank(val))
continue;
message = message.replace(el, val);
}
} catch (Exception e) {
log.error("验证引擎进行数据校验时出现异常, message:{}", message, e);
}
return message;
}
}
使用
//调用验证方法获得验证结果
HybValidatorResult bvr = HybValidator.validate(emp, CreateValidator.class);
//表示有错误
if (bvr.isErrors()) {
}
//资源文件内容
//my.empty.message=不能为空
//user.name.empty=用户名
方式二:用spring自带的@Validated,无需调用验证方法
这里有个问题:@Validated注解不认注解@NotEmpty中的key,如何解决呢?
最终的实现方案:自定义验证器
代码:
注解
@Documented
@Target({FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@ReportAsSingleViolation
//指定验证器
@Constraint(validatedBy = NotEmptyValidator.class)
public @interface NotEmpty { String message() default "{my.empty.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { }; String key() default "";
}
验证器:自定义
public class NotEmptyValidator extends AbstractValidator<NotEmpty,Object>{
@Override
public void initialize(NotEmpty notEmpty) {
}
@Override
public boolean doIsValid(Object value, ConstraintValidatorContext cc) {
return value != null;
}
}
/**
* 这里采用模板的设计模式
* @param constraintAnnotation
*/
public abstract class AbstractValidator<A extends Annotation,T> implements ConstraintValidator<A,T>{
/**
* 初始化由具体类实现
* @param constraintAnnotation
*/
@Override
public abstract void initialize(A constraintAnnotation);
/**
* 初始化具体由实现类实现
* @param value
* @param context
* @return
*/
@Override
public boolean isValid(T value, ConstraintValidatorContext context){
//获取验证结果,采用模板方法
boolean result = doIsValid(value,context);
//当验证错误时修改默认信息
if(!result){
//改变默认提示信息
if(ConstraintValidatorContextImpl.class.isAssignableFrom(context.getClass())){
ConstraintValidatorContextImpl constraintValidatorContext =
(ConstraintValidatorContextImpl)context;
//获取默认提示信息
String defaultConstraintMessageTemplate =
context.getDefaultConstraintMessageTemplate();
Object key =
constraintValidatorContext.getConstraintDescriptor().getAttributes().get("key");
//禁用默认提示信息
context.disableDefaultConstraintViolation();
//设置提示语(在message前面加上key)
context.buildConstraintViolationWithTemplate(key + defaultConstraintMessageTemplate).addConstraintViolation();
}
}
return result;
}
/**
* 真正验证方法
* @param value
* @param context
* @return
*/
public abstract boolean doIsValid(T value, ConstraintValidatorContext context);
}
使用:
调用的时候只要在JavaBean前加上@Validated注解即可
总结:上述就是在工作中遇到的问题,并扩展了Validator
Springboot集成BeanValidation扩展一:错误提示信息加公共模板的更多相关文章
- Springboot集成BeanValidation扩展二:加载jar中的资源文件
一.需求 今天在搭建Springboot框架的时候,又遇到一个需求:在多模块系统中,有些模块想自己管理BeanValidation的资源文件(默认是启动项目claspath下的 ValidationM ...
- Springboot 集成 Thymeleaf 及常见错误
Thymeleaf模板引擎是springboot中默认配置,与freemarker相似,可以完全取代jsp,在springboot中,它的默认路径是src/main/resources/templat ...
- springboot集成mybatis源码分析-启动加载mybatis过程(二)
1.springboot项目最核心的就是自动加载配置,该功能则依赖的是一个注解@SpringBootApplication中的@EnableAutoConfiguration 2.EnableAuto ...
- springboot 集成apollo,根据不同环境加载配置
- SpringBoot集成Shiro 实现动态加载权限
一.前言 本文小编将基于 SpringBoot 集成 Shiro 实现动态uri权限,由前端vue在页面配置uri,Java后端动态刷新权限,不用重启项目,以及在页面分配给用户 角色 . 按钮 .ur ...
- Springboot学习04-默认错误页面加载机制源码分析
Springboot学习04-默认错误页面加载机制源码分析 前沿 希望通过本文的学习,对错误页面的加载机制有这更神的理解 正文 1-Springboot错误页面展示 2-Springboot默认错误处 ...
- SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)
1.pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...
- springboot集成websocket实现向前端浏览器发送一个对象,发送消息操作手动触发
工作中有这样一个需示,我们把项目中用到代码缓存到前端浏览器IndexedDB里面,当系统管理员在后台对代码进行变动操作时我们要更新前端缓存中的代码怎么做开始用想用版本方式来处理,但这样的话每次使用代码 ...
- SpringBoot中BeanValidation数据校验与优雅处理详解
目录 本篇要点 后端参数校验的必要性 不使用Validator的参数处理逻辑 Validator框架提供的便利 SpringBoot自动配置ValidationAutoConfiguration Va ...
随机推荐
- php中普通方法和静态方法的区别以及抽象类和接口
实例化类产生对象.class fenbi{ //普通成员,属于对象 public $length = "10cm"; //静态成员,静态变量,属于类. public static ...
- HDU 1937 J - Justice League
J - Justice League Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- Java常用实体类
System类 访问系统属性 - 遍历 package org.zln.usefulclass.system; import java.util.Properties; /** * Created b ...
- Android性能优化之避免内存泄漏的建议
在android程序开发中,内存泄漏问题是比较常见的问题,相信有过一些android编程经历的程序猿都遇到过各种各样的内存泄漏.内存泄漏是造成应用程序OOM的主要原因之一,是编程中必须避免的问题.下面 ...
- hdu 1564 Play a game (博弈)
Play a game Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 1534 Schedule Problem (差分约束)
Schedule Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- cf 834 E. Ever-Hungry Krakozyabra
cf 834 E. Ever-Hungry Krakozyabra(爆搜+数位dp) 题意: 定义一种inedible tail为一个数把每一位数字按不降的顺序排列后,去掉前导0组成的序列 比如570 ...
- 洛谷 P3396 哈希冲突 解题报告
P3396 哈希冲突 题目背景 此题约为NOIP提高组Day2T2难度. 题目描述 众所周知,模数的hash会产生冲突.例如,如果模的数p=7,那么4和11便冲突了. B君对hash冲突很感兴趣.他会 ...
- poj 2186 强连通入门题目
每头牛的梦想就是成为牛群中最受欢迎的牛. 在一群N(1 <= N <= 10,000)母牛中, 你可以得到M(1 <= M <= 50,000)有序的形式对(A,B),告诉你母 ...
- 使用org.jsoup.Jsoup下载网络中的图片
package com.enation.newtest; import java.io.BufferedOutputStream; import java.io.File; import java.i ...