java 自定义注解 校验经纬度

本文介绍了如何在Java开发中利用Hibernate Validator自定义注解进行经纬度的合法性校验,包括创建注解、实现校验逻辑及在字段上应用注解的方法,简化了代码中频繁的if条件判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在日常的开发中可能会对手机号、邮箱、身份证号、经纬度的合法性进行校验,然而每次使用if语句调用校验函数进行校验又显得过于麻烦,这时候自定义校验注解用处就很大了。

1、需要的依赖

<!-- hibernate validator -->
<dependency>
   <groupId>javax.validation</groupId>
   <artifactId>validation-api</artifactId>
   <version>2.0.1.Final</version>
 </dependency>

2、自定义校验注解

  在自定义注解之前先来了解一下注解的基础知识:

  元注解:@interface上面按需要注解上一些东西,包括@Retention、@Target、@Document、@Inherited四种。

  注解的保留策略:

@Retention(RetentionPolicy.SOURCE) // 注解仅存在于源码中,在class字节码文件中不包含

@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得

@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到

   注解的作用目标:

@Target(ElementType.TYPE) // 接口、类、枚举、注解

@Target(ElementType.FIELD) // 字段、枚举的常量

@Target(ElementType.METHOD) // 方法

@Target(ElementType.PARAMETER) // 方法参数

@Target(ElementType.CONSTRUCTOR) // 构造函数

@Target(ElementType.LOCAL_VARIABLE) // 局部变量

@Target(ElementType.ANNOTATION_TYPE) // 注解

@Target(ElementType.PACKAGE) // 包
   
(1)创建注解类并对注解记性赋值

import javax.validation.Constraint;
import javax.validation.Payload;
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)
@Constraint(validatedBy = CheckLatitudeValidator.class) //校验的逻辑处理类
public @interface CheckLatitude {

    String message() default "请输入正确的纬度格式,精确到小数点后六位";   //提示的信息

    Class<?>[] groups() default { };  //分组验证,例如只在新增时进行校验等

    Class<? extends Payload>[] payload() default { };

(2)处理校验逻辑

校验纬度

/**
 * @ClassName CheckLatitudeValidator
 * @Description 经纬度校验
 * 通过实现ConstraintValidator,来实现自定义校验逻辑
 **/
public class CheckLatitudeValidator implements ConstraintValidator<CheckLatitude, BigDecimal> {
    @Override
    public void initialize(CheckLatitude myConstraint) {
        /* 初始化数据,如果有需要初始化的在此操作,没有则不需操作次函数 */
    }

    /**
     * @Description: 自定义校验逻辑
     */
    @Override
    public boolean isValid(BigDecimal checkLatitude, ConstraintValidatorContext constraintValidatorContext) {
        /*  逻辑代码,不符合返回false,否则返回true */
        boolean flag = false;
        try {
            if (checkLatitude != null) {
                String source = checkLatitude.toString();
               
               Pattern pattern = Pattern.compile("((?:[0-9]|[1-8][0-9]|90)\\.([0-9]{6}))");
                if (pattern.matcher(source).matches()) {
                    flag = true;
                }
            }
        } catch (Exception e) {
            e.getMessage();
        }
        return flag;
    }
}

校验经度

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.math.BigDecimal;
import java.util.regex.Pattern;

/**
 * @ClassName CheckLongitudeValidator
 * @Description 经纬度校验
 * 通过实现ConstraintValidator,来实现自定义校验逻辑
 * 
 **/
public class CheckLongitudeValidator implements ConstraintValidator<CheckLongitude, BigDecimal> {
    @Override
    public void initialize(CheckLongitude myConstraint) {
        /* 初始化数据,如果有需要初始化的在此操作,没有则不需操作次函数 */
    }

    /**
     * @Description: 自定义校验逻辑
     */
    @Override
    public boolean isValid(BigDecimal checkLongitude, ConstraintValidatorContext constraintValidatorContext) {
        /*  逻辑代码,不符合返回false,否则返回true */
        boolean flag = false;
        try {
            if (checkLongitude != null) {
                String source = checkLongitude.toString();
             
                Pattern pattern = Pattern.compile("((?:[0-9]|[1-9][0-9]|1[0-7][0-9]|180)\\.([0-9]{6}))");
                if (pattern.matcher(source).matches()) {
                    flag = true;
                }
            }
        } catch (Exception e) {
            e.getMessage();
        }
        return flag;
    }
}

(3)使用自定义注解

在所需要校验的字段上使用注解

————————————————————————————————————————

另外:
只校验正数 0-90.000000 0-180.000000 范围内
经纬度校验
经度longitude: (?:[0-9]|[1-9][0-9]|1[0-7][0-9]|180)\.([0-9]{6})
纬度latitude: (?:[0-9]|[1-8][0-9]|90)\.([0-9]{6})
——————————————————————————————————————————
经度: -180.0~+180.0(整数部分为0~180,必须输入1到5位小数)
正则表达式:

/^[\-\+]?(0?\d{1,2}\.\d{1,5}|1[0-7]?\d{1}\.\d{1,5}|180\.0{1,5})$/

纬度: -90.0~+90.0(整数部分为0~90,必须输入1到5位小数)

正则表达式: /^[\-\+]?([0-8]?\d{1}\.\d{1,5}|90\.0{1,5})$/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值