java注解验证实体_Java通过注解和反射 实现模拟 Hibernate Validator验证框架对实体对象的字段验证功能...

需求:

1实现对字段的非空校验

2实现对邮箱的正则验证

3实现对年龄字段的未成年判断

输出:

若字段为空则打印注解传递的message

若邮箱格式正则验证不通过则输出邮箱格式错误

若年龄小于18则打印注解传递的message

1 创建一个实体类(getter&&setter略)【下面的所有文件都在同一个包中】

public class User {

@NotNull()

String userid;

@NotNull(message = "账号为空")

String username;

@Regular("^(\\w-*\\.*)+@(\\w-?)+(\\.\\w{2,})+$")

String email;

@NotAdult(message = "该用户未成年")

int age;

public User() {

}

public User(String userid, String username, String email, int age) {

this.userid = userid;

this.username = username;

this.age = age;

this.email = email;

}

}

2 创建三个注解接口(NotNull,Regular,NotAdult)

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

* 验证字段是否不是空

*/

@Target(ElementType.FIELD) //作用于字段

@Retention(RetentionPolicy.RUNTIME) //作用于运行时

public @interface NotNull {

String message() default "null";//为空时的提示信息,不输入默认为null

}

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

* 正则验证

*/

@Target(ElementType.FIELD) //作用于字段

@Retention(RetentionPolicy.RUNTIME) //作用于运行时

public @interface Regular {

String value() ;//正则表达式,必须输入

}

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

* 验证用户是否未成年

*/

@Target(ElementType.FIELD) //作用于字段

@Retention(RetentionPolicy.RUNTIME) //作用于运行时

public @interface NotAdult {

String message();//未成年的提示信息,必须要输入

}

3 实现注解功能的框架类

import java.lang.reflect.Field;

import java.util.regex.Pattern;

public class MyHibernateValidator {

public static void main(String[] args) throws Exception {

//这里本应该自动扫描全部类遍历,时间原因就没有深入完成了,就手动测试下

//输入四种不同的情况查看结果

completeAnnotation(new User("001","张三","123zs@qq.com",18));

completeAnnotation(new User("","张三","123zs.",17));

completeAnnotation(new User("","","@qq.com.",22));

completeAnnotation(new User("","","",15));

}

//通过泛型方法传递类对象 完成注解的功能

public static void completeAnnotation(T t){

//获取要操作的实体类的字节码文件对象

Class> opClass = t.getClass();

//获取这个对象的所有字段

Field[] fields = opClass.getDeclaredFields();

System.out.println("------------开始分隔符-无输出内容则说明验证通过-----------");

//遍历所有字段,判断字段是否使用了注解

for(Field f : fields){

try{

Object fieldValue = f.get(t);//根据类对象获取字段值对象

//获取注解对象

NotNull notNull = f.getAnnotation(NotNull.class); //根据字段对象获取NotNull注解对象

Regular regular = f.getAnnotation(Regular.class); //根据字段对象获取Regular注解对象

NotAdult notAdult = f.getAnnotation(NotAdult.class); //根据字段对象获取NotAdult注解对象

if(f.isAnnotationPresent(NotNull.class)){

if(fieldValue == null || fieldValue.toString() == ""){

System.out.println(f.getName()+"\t"+notNull.message());//通过message打印信息

}

}

if(f.isAnnotationPresent(Regular.class)){

if(fieldValue != null && fieldValue.toString() != "" ) {

String email = fieldValue.toString();

if (!Pattern.matches(regular.value(), email)) {

System.out.println(f.getName()+"\t"+"email格式错误");

}

}

}

//int类型不能为空,不需要判断

if(f.isAnnotationPresent(NotAdult.class)){

if(fieldValue != null && Integer.parseInt(fieldValue.toString()) < 18){

System.out.println(f.getName()+"\t"+notAdult.message());

}

}

}catch (NullPointerException | IllegalAccessException e){

e.printStackTrace();

}

}

}

}

输出结果

9f2e2764b28487bfed298bbca38f5dd1.png

本文地址:https://blog.csdn.net/c_o_d_e_/article/details/107167320

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值