This action will force synchronization from Sir丶雨轩/ehi-verification, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
一个好用的快速的参数校验框架 支持直接调用校验并提供Spring-webmvc拦截器对请求参数进行校验
没啥架构,不强制依赖第三方Jar包
PS:正在往Maven中央仓库发布中 暂时使用拉取项目打包的本地的方式使用
<dependency>
<groupId>com.yuxuan66</groupId>
<artifactId>ehi-verification</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
如非Maven项目直接打包后找到Jar放入项目中即可
Verification,Param,Len
EhiVerification,VerificationResult,Result,ConstFormat
Param:标记一个参数要进行校验
参数:
value(String)=字段名称
require(boolean)=是否需要(默认为需要)
asFor(String)=必须跟asFor的字段内容一致
nullFor(String)=nullFor的字段内容和当前内容不能同时为空
nullMsg(String)=字段为空的错误提示,默认使用全局提示
errorMsg(String)=字段校验不通过的错误提示,默认使用全局提示
pattern(String)=用来校验的正则
len(@Len)=长度校验
Verification:标记一个类,Bean只有此注解才会生效 参数: params(@Param)=字段列表
@Verification(params = {
@Param(value = "name", require = false, len = @Len(min = 10, max = 20),errorMsg = "用户名必须为10~20位字符"),
@Param(value = "password"),
@Param(value = "email", pattern = ConstFormat.EMAIL)
})
public class User extends HashMap {
private String name;
private String password;
private String email;
@Param(value = "age")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
demo
public static void main(String ...args){
User user = new User();
user.setName("1111111111");
user.setEmail("eqeq@qq.com");
user.setPassword("dsadsa");
VerificationResult verificationResult = new EhiVerification().verification(user);
System.out.println(verificationResult.isVerification());
System.out.println(verificationResult.getErrorMsg());
}
//校验一个实体 实体可以集成Map会自动区分
VerificationResult verificationResult = new EhiVerification().verification(user);
返回一个校验结果
verificationResult.isVerification();//返回是否有校验不通过字段
verificationResult.getErrorMsg();//返回校验不通过的提示语,如上行代码返回true则此代码返回空字符串
下面我们来学习一下高级的使用方式,这里我们使用springboot进行集成
package com.km66.knowledge.support.config.web;
import com.km66.knowledge.support.interceptor.AuthInterceptor;
import com.km66.knowledge.support.resolver.DictArgumentResolver;
import com.yuxuan66.ehi.verification.core.EhiVerification;
import com.yuxuan66.ehi.verification.interceptor.ParamInterceptor;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//创建核心校验器
EhiVerification ehiVerification = new EhiVerification();
//添加参数拦截器
registry.addInterceptor(new ParamInterceptor(ehiVerification));
}
}
@RequestMapping(path = "updateAccessTokenByRefreshToken", method = RequestMethod.POST)
@ResponseBody
@Verification(params = {
@Param(value = "username", len = @Len(min = 10, max = 20),errorMsg = "用户名格式不正确",nullMsg = "用户名必须输入"),
@Param(value = "email" ,pattern = ConstFormat.EMAIL),
@Param(value = "password")
})
public RespEntity updateAccessTokenByRefreshToken(@RequestParam Map<String, Object> params) {
try {
return usersService.updateAccessTokenByRefreshToken(params);
} catch (Exception e) {
logger.error("服务器异常", e);
return RespEntity.fail();
}
}
交流群:795272647
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。