使用SpringBoot通过自定义注解+AOP+全局异常处理实现参数统一非空校验

一、前言

        在我们写后台接口时,难免对参数进行非空校验,如果一两个还好,但如果需要写大量的接口,及必填参数太多的时候,会给我们开发带来大量的重复工作,及很多相似代码。而sping自带的@RequestParam注解并不能完全满足我们的需求,因为这个注解只会校验请求中是否存在该参数,而不会校验这个参数的值是nulll还是空字符串(""),如果参数不存在则会抛出org.springframework.web.bind.MissingServletRequestParameterException异常。虽然目前已经有许多成熟的校验框架,功能丰富,但是我们只需要做一个非空校验即可。

        因此我们可以自定义 一个注解用于校验参数是否为空。

使用的的框架

  • spring boot:1.5.9.RELEASE
  • JDK:1.8

二、准备工作

        首先需要创建一个spring boot项目,并引入相关maven依赖(主要是spring-boot-starter-web与aspectjweaver),pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.beauxie</groupId>
    <artifactId>param-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>param-demo</name>
    <description>param-demo for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 添加支持web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入AOP相应的注解-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

说明:

  • spring-boot-starter-web用于spring boot WEB支持
  • aspectjweaver 用于引入aop的相关的注解,如@Aspect@Pointcut

三、自定义注解实现统一校验

总体思路:自定义一个注解,对必填的参数加上该注解,然后定义一个切面,校验该参数是否为空,如果为空则抛出自定义的异常,该异常被自定义的异常处理器捕获,然后返回相应的错误信息。

1. 自定义注解

创建一个名为'ParamCheck'的注解,代码如下:

package com.beauxie.param.demo.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * "参数不能为空"注解,作用于方法参数上。
 * 
 * @author Beauxie
 * @date Created on 2017/1/6
 */
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface ParamCheck {
    /**
     * 是否非空,默认不能为空
     */
    boolean notNull() default true;
}

说明:

  • @Target(ElementType.PARAMETER)表示该注解作用于方法参数上
  • 该类可以拓展,比如增加length校验

2. 自定义异常类

这个异常类与自定义注解配合一起使用,当加上'@ParamCheck'的参数为空时,抛出该异常,代码如下:

package com.beauxie.param.demo.exception;

/**
 * @author Beauxie
 * @date Created on 2017/1/6
 */
public class ParamIsNullException extends RuntimeException {
    private final String parameterName;
    private final String parameterType;

    public ParamIsNullException(String parameterName, String parameterType) {
        super("");
        this.parameterName = parameterName;
        this.parameterType = parameterType;
    }

    @Override
    public String getMessage() {
        return "Required " + this.parameterType + " parameter \'" + this.parameterName + "\' must be not null !";
    }

    public final String getParameterName() {
        return this.parameterName;
    }

    public final String getParameterType() {
        return this.parameterType;
    }
}

说明:

  • 该异常继承RuntimeException,并定义了两个成员属性、重写了getMessage()方法
  • 之所以自定义该异常,而不用现有的org.springframework.web.bind.MissingServletRequestParameterException类,是因为MissingServletRequestParameterException为Checked异常,在动态代理过程中,很容易引发java.lang.reflect.UndeclaredThrowableException异常。

3. 自定义AOP

代码如下:

package com.beauxie.param.demo.aop;

import com.beauxie.param.demo.annotation.ParamCheck;
import com.beauxie.param.demo.exception.ParamIsNullException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @author Beauxie
 * @date Created on 2017/1/6
 */
@Component
@Aspect
public class ParamCheckAop {
    private Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * 定义有一个切入点,范围为web包下的类
     */
    @Pointcut("execution(public * com.beauxie.param.demo.web..*.*(..))")
    public void checkParam() {
    }

    @Before("checkParam()")
    public void doBefore(JoinPoint joinPoint) {
    }

    /**
     * 检查参数是否为空
     */
    @Around("checkParam()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {

        MethodSignature signature = ((MethodSignature) pjp.getSignature());
        //得到拦截的方法
        Method method = signature.getMethod();
        //获取方法参数注解,返回二维数组是因为某些参数可能存在多个注解
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        if (parameterAnnotations == null || parameterAnnotations.length == 0) {
            return pjp.proceed();
        }
        //获取方法参数名
        String[] paramNames = signature.getParameterNames();
        //获取参数值
        Object[] paranValues = pjp.getArgs();
        //获取方法参数类型
        Class<?>[] parameterTypes = method.getParameterTypes();
        for (int i = 0; i < parameterAnnotations.length; i++) {
            for (int j = 0; j < parameterAnnotations[i].length; j++) {
                //如果该参数前面的注解是ParamCheck的实例,并且notNull()=true,则进行非空校验
                if (parameterAnnotations[i][j] != null && parameterAnnotations[i][j] instanceof ParamCheck && ((ParamCheck) parameterAnnotations[i][j]).notNull()) {
                    paramIsNull(paramNames[i], paranValues[i], parameterTypes[i] == null ? null : parameterTypes[i].getName());
                    break;
                }
            }
        }
        return pjp.proceed();
    }

    /**
     * 在切入点return内容之后切入内容(可以用来对处理返回值做一些加工处理)
     *
     * @param joinPoint
     */
    @AfterReturning("checkParam()")
    public void doAfterReturning(JoinPoint joinPoint) {
    }

    /**
     * 参数非空校验,如果参数为空,则抛出ParamIsNullException异常
     * @param paramName
     * @param value
     * @param parameterType
     */
    private void paramIsNull(String paramName, Object value, String parameterType) {
        if (value == null || "".equals(value.toString().trim())) {
            throw new ParamIsNullException(paramName, parameterType);
        }
    }

}

4. 全局异常处理器

该异常处理器捕获在ParamCheckAop类中抛出的ParamIsNullException异常,并进行处理,代码如下:

package com.beauxie.param.demo.exception;

import com.beauxie.param.demo.common.Result;
import com.beauxie.param.demo.enums.EnumResultCode;
import com.beauxie.param.demo.utils.ResponseMsgUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理.
 * 一般情况下,方法都有异常处理机制,但不能排除有个别异常没有处理,导致返回到前台,因此在这里做一个异常拦截,统一处理那些未被处理过的异常
 *
 * @author Beauxie
 * @date Created on 2017/1/6
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    /**
     * 参数为空异常处理
     *
     * @param ex
     * @return
     */
    @ExceptionHandler({MissingServletRequestParameterException.class, ParamIsNullException.class})
    public Result<String> requestMissingServletRequest(Exception ex) {
        LOGGER.error("request Exception:", ex);
        return ResponseMsgUtil.builderResponse(EnumResultCode.FAIL.getCode(), ex.getMessage(), null);
    }

    /**
     * 特别说明: 可以配置指定的异常处理,这里处理所有
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Result<String> errorHandler(HttpServletRequest request, Exception e) {
        LOGGER.error("request Exception:", e);
        return ResponseMsgUtil.exception();
    }
}

说明:

  • requestMissingServletRequest()方法上加上@ExceptionHandler({MissingServletRequestParameterException.class, ParamIsNullException.class})注解,表明只处理处理MissingServletRequestParameterExceptionParamIsNullException异常
  • errorHandler()方法则处理其他的异常

四、测试

com.beauxie.param.demo.web包下新建一个名为HelloController的类,用于测试,代码如下:

package com.beauxie.param.demo.web;

import com.beauxie.param.demo.annotation.ParamCheck;
import com.beauxie.param.demo.common.Result;
import com.beauxie.param.demo.enums.EnumResultCode;
import com.beauxie.param.demo.utils.ResponseMsgUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Beauxie
 * @date Created on 2018/1/6
 */
@RestController
public class HelloController {
    /**
     *测试@RequestParam注解
     * @param name
     * @return
     */
    @GetMapping("/hello1")
    public Result<String> hello1(@RequestParam String name) {
        return ResponseMsgUtil.builderResponse(EnumResultCode.SUCCESS.getCode(), "请求成功", "Hello," + name);
    }

    /**
     * 测试@ParamCheck注解
     * @param name
     * @return
     */
    @GetMapping("/hello2")
    public Result<String> hello2(@ParamCheck String name) {
        return ResponseMsgUtil.builderResponse(EnumResultCode.SUCCESS.getCode(), "请求成功", "Hello," + name);
    }

    /**
     * 测试@ParamCheck与@RequestParam一起时
     * @param name
     * @return
     */
    @GetMapping("/hello3")
    public Result<String> hello3(@ParamCheck @RequestParam String name) {
        return ResponseMsgUtil.builderResponse(EnumResultCode.SUCCESS.getCode(), "请求成功", "Hello," + name);
    }

最后运行ParamDemoApplicatio的main方法,打开浏览器进行测试。

1. 测试@RequestParam注解

  • 参数名为空测试

    在浏览器的地址栏输入:http://localhost:8080/hello1,结果如下:

    RequestParam1

    后台错误信息输出:
    Error1

    此时后台会报org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present错误信息,提示参数'name'不存在

  • 参数名不为空,值为空测试

    在浏览器的地址栏输入:http://localhost:8080/hello1?name=,结果如下:

    RequestParam2


    此时,name的值为空,但请求结果正常返回。

  • 参数名与值都不为空测试

    在浏览器的地址栏输入:http://localhost:8080/hello1?name=Beauxie,结果如下:

    RequestParam3

2. 测试@ParamCheck注解

  • 参数名为空测试

    在浏览器的地址栏输入:http://localhost:8080/hello2,结果如下:
    [图片上传失败...(image-f11449-1515228018125)]
    后台错误信息输出:

    Error2

  • 参数名不为空,值为空测试

    在浏览器的地址栏输入:http://localhost:8080/hello2?name=,结果如下:
    [图片上传失败...(image-8d0648-1515228018125)]
    此时,name的值为空,请求结果y提示参数name的值不能为空。
    后台错误信息输出:

    Error3

  • 参数名与值都不为空测试

    在浏览器的地址栏输入:http://localhost:8080/hello2?name=Beauxie,结果如下:

    ParamCheck3

3. 测试总结

  • 当参数名为空时,分别添加两个注解的接口都会提示参数不能为空
  • 当参数名不为空,值为空时,@RequestParam注解不会报错,但@ParamCheck注解提示参数'name'的值为空

五、总结

  • 经过以上的测试也验证了@RequestParam只会验证对应的参数是否存在,而不会验证值是否为空
  • ParamCheck还可以进行拓展,比如参数值长度、是否含有非法字符等校验





六、源码下载地址

由于csdn下载需要积分,因此添加github源码地址:

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,200评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,526评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,321评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,601评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,446评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,345评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,753评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,405评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,712评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,743评论 2 314
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,529评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,369评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,770评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,026评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,301评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,732评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,927评论 2 336

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,517评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,680评论 6 342
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,142评论 25 707
  • 这世界上最可怕的付出就是:我都是为了你。 我的朋友小斯,最近辞职了。 她从刚毕业就在这家公司服务,14年了。从职场...
    江來阅读 207评论 0 2
  • 当时间被回顾时会发现时光荏苒,不过弹指一挥间,特别是当自己忙忙碌碌却没有多大实际作为时,一种愤懑、悲凉的情绪涌...
    天空里的飞鸟阅读 2,943评论 1 50