| public class ValidationUtil { |
| |
| private static Validator failFastValidator; |
| |
| static { |
| validator = Validation.buildDefaultValidatorFactory().getValidator(); |
| |
| failFastValidator = Validation.byProvider(HibernateValidator.class) |
| .configure().failFast(true) |
| .buildValidatorFactory().getValidator(); |
| } |
| |
| public static List<String> validFailFast(UserInfo userInfo, Class<?>... groups){ |
| |
| Set<ConstraintViolation<UserInfo>> set = failFastValidator.validate(userInfo,groups); |
| List<String> list = set.stream().map(v -> "属性:" + v.getPropertyPath() + |
| ",属性的值:" |
| + v.getInvalidValue() + ",校验不通过的提示信息:" + v.getMessage() |
| +",消息模板:"+v.getMessageTemplate()) |
| .collect(Collectors.toList()); |
| return list; |
| } |
| } |
| |
| # 测试 |
| public class ValidationTest { |
| public static void main(String[] args) { |
| UserInfo userInfo = new UserInfo(); |
| userInfo.setName("zhaodaowen"); |
| userInfo.setAge(18); |
| userInfo.setEmail("2025186764@qq.com"); |
| userInfo.setPhone("13312345678"); |
| userInfo.setBirthDay(LocalDateTime.now().minusDays(1)); |
| userInfo.setPersonalPage("http://www.roadjava.com"); |
| List<String> list = ValidationUtil.validFailFast(userInfo,UserInfo.Add.class,Default.class); |
| System.out.print(list); |
| } |
| } |
| public class ValidationUtil { |
| |
| private static ExecutableValidator executableValidator; |
| |
| static { |
| validator = Validation.buildDefaultValidatorFactory().getValidator(); |
| |
| failFastValidator = Validation.byProvider(HibernateValidator.class) |
| .configure().failFast(true) |
| .buildValidatorFactory().getValidator(); |
| |
| executableValidator = validator.forExecutables(); |
| } |
| |
| public static <T> List<String> validNotBean(T object, |
| Method method, |
| Object[] parameterValues, |
| Class<?>... groups) { |
| Set<ConstraintViolation<T>> set = executableValidator.validateParameters(object, method, parameterValues, groups); |
| List<String> list = set.stream().map(v -> "属性:" + v.getPropertyPath() + |
| ",属性的值:" |
| + v.getInvalidValue() + ",校验不通过的提示信息:" + v.getMessage() |
| +",消息模板:"+v.getMessageTemplate()) |
| .collect(Collectors.toList()); |
| return list; |
| } |
| } |
| |
| public class UserInfoService { |
| |
| |
| |
| |
| |
| public String getByName(@NotNull String name) { |
| |
| StackTraceElement st = Thread.currentThread().getStackTrace()[1]; |
| String methodName = st.getMethodName(); |
| Method method = null; |
| try { |
| method = this.getClass().getDeclaredMethod(methodName, String.class); |
| }catch (Exception e) { |
| e.printStackTrace(); |
| } |
| List<String> list = ValidationUtil.validNotBean(this, method, new Object[]{name}); |
| |
| System.out.println(list); |
| return "ok"; |
| } |
| } |
| |
| # 测试 |
| public class App { |
| public static void main(String[] args) { |
| UserInfoService userInfoService = new UserInfoService(); |
| userInfoService.getByName(null); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下