使用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文件如下:


<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.0modelVersion>

    <groupId>com.beauxiegroupId>
    <artifactId>param-demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>param-demoname>
    <description>param-demo for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.9.RELEASEversion>
        <relativePath/> 
    parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.8.5version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            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 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 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 hello1(@RequestParam String name) {
        return ResponseMsgUtil.builderResponse(EnumResultCode.SUCCESS.getCode(), "请求成功", "Hello," + name);
    }

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

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

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

1. 测试@RequestParam注解

  • 参数名为空测试
    在浏览器的地址栏输入:http://localhost:8080/hello1,结果如下:
    使用SpringBoot通过自定义注解+AOP+全局异常处理实现参数统一非空校验_第1张图片
    后台错误信息输出:
    使用SpringBoot通过自定义注解+AOP+全局异常处理实现参数统一非空校验_第2张图片
    此时后台会报org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present错误信息,提示参数’name’不存在
  • 参数名不为空,值为空测试
    在浏览器的地址栏输入:http://localhost:8080/hello1?name=,结果如下:
    使用SpringBoot通过自定义注解+AOP+全局异常处理实现参数统一非空校验_第3张图片

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

  • 参数名与值都不为空测试
    在浏览器的地址栏输入:http://localhost:8080/hello1?name=Beauxie,结果如下:
    使用SpringBoot通过自定义注解+AOP+全局异常处理实现参数统一非空校验_第4张图片

2. 测试@ParamCheck注解

  • 参数名为空测试
    在浏览器的地址栏输入:http://localhost:8080/hello2,结果如下:
    使用SpringBoot通过自定义注解+AOP+全局异常处理实现参数统一非空校验_第5张图片
    后台错误信息输出:
    Error2
  • 参数名不为空,值为空测试
    在浏览器的地址栏输入:http://localhost:8080/hello2?name=,结果如下:
    使用SpringBoot通过自定义注解+AOP+全局异常处理实现参数统一非空校验_第6张图片
    此时,name的值为空,请求结果y提示参数name的值不能为空。
    后台错误信息输出:
    Error3

  • 参数名与值都不为空测试
    在浏览器的地址栏输入:http://localhost:8080/hello2?name=Beauxie,结果如下:
    使用SpringBoot通过自定义注解+AOP+全局异常处理实现参数统一非空校验_第7张图片

3. 测试总结

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

五、总结

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

六、源码下载地址

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

  • github: https://github.com/Beauxie/projects-demo
  • csdn: http://download.csdn.net/download/beauxie/10191744

你可能感兴趣的:(Spring,boot)