当前位置: 移动技术网 > IT编程>开发语言>Java > java自定义注解实现前后台参数校验的实例

java自定义注解实现前后台参数校验的实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

其实是可以通过@constraint来限定自定义注解的方法。

@constraint(validatedby = xxxx.class)

下面是我做的 java自定义注解实现前后台参数校验 的代码示例

对这个感兴趣的,请好好看,好好学:

package sonn.sonnannotation;

import java.lang.annotation.documented;
import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;

import javax.validation.constraint;
import javax.validation.constraintvalidator;
import javax.validation.constraintvalidatorcontext;
import javax.validation.payload;

import sonn.util.stringutill;

/**
* @classname: isvalidstring 
* @description: 自定义注解实现前后台参数校验,判断是否包含非法字符
* @author 无名
* @date 2016-7-25 下午8:22:58 
* @version 1.0
 */
@target({elementtype.field, elementtype.method})
@retention(retentionpolicy.runtime)
@constraint(validatedby = isvalidstring.validstringchecker.class)
@documented
public @interface isvalidstring 
{
  string message() default "the string is invalid.";
  
  class<?>[] groups() default {};
  
  class<? extends payload>[] payload() default{};
  
  class validstringchecker implements constraintvalidator<isvalidstring,string>
  {

    @override
    public void initialize(isvalidstring arg0)
    {  
    }

    @override
    public boolean isvalid(string strvalue, constraintvalidatorcontext context)
    {
      if(stringutill.isstringempty(strvalue))
      {
        return true;
      }
      if(strvalue.contains("<"))
      {
        return false;
      }
      return true;
    }
    
  }
}

上述代码,通过@constraint(validatedby = isvalidstring.validstringchecker.class)限定了注解的方法逻辑---该注解类的名为validstringchecker的内部类。

而该内部类实现了constraintvalidator<isvalidstring,string>接口

官方文档是这样描述的:

javax.validation
interface constraintvalidator<a extends annotation,t>

------------------------------------------------

public interface constraintvalidator<a extends annotation,t>defines the logic to validate a given constraint a for a given object type t.
implementations must comply to the following restriction:

•t must resolve to a non parameterized type
•or generic parameters of t must be unbounded wildcard types
the annotation supportedvalidationtarget can be put on a constraintvalidator implementation to mark it as supporting cross-parameter constraints. check out supportedvalidationtarget and constraint for more information.

实现的isvalid方法便是,该接口的校验方法。

试验一下效果,在要校验的实体类字段加上注解:

写文章页面,文章标题内加入'<'然后提交:

提交失败,报500错误,说明注解生效:

但这样还有问题,我的blog网站不能直接打印出报错信息。还是要搞一个error页面出来。

这个简单,web.xml下加入error页面路径,然后做一个页面即可:

<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>

首先介绍些基本概念:

1.java用@interface xx{}定义一个注解。

注解这个东西,其实并不神秘,不过是一种标记,程序运行到标记处,就执行相应逻辑罢了。注解本身即是一个类。

2.注解在定义时,标注一些注解可以表示特定意义:

@retention(retentionpolicy.source) // 注解仅存在于源码中,在class字节码文件中不包含

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

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

(runtime的值得注意下,因为意味着可以反射来获取)

@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) // 包

有一种做法就是在定义注解时加上@taget(xx)和@retention(retentionpolicy.runtime) ,但没有在注解中写方法,只是在运行时通过反射机制来获取注解,然后自己写相应逻辑(所谓注解解析器)

大概是类似的写法:

import java.lang.annotation.documented;
import java.lang.annotation.elementtype;
import java.lang.annotation.inherited;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;

@documented
@inherited
@target({ elementtype.field, elementtype.method })
@retention(retentionpolicy.runtime)
public @interface validate
{
  public int min() default 1;

  public int max() default 10;

  public boolean isnotnull() default true;
}

之后运行时,用反射获取注解,具体不谈。

之前在网上查找这方面技术文章找到的都是这种,给当时的我带来很大困惑。觉得不是我想要的。

以上这篇java自定义注解实现前后台参数校验的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网