詳解使用Spring AOP和自定義注解進行參數(shù)檢查
引言
使用SpringMVC作為Controller層進行Web開發(fā)時,經(jīng)常會需要對Controller中的方法進行參數(shù)檢查。本來SpringMVC自帶@Valid和@Validated兩個注解可用來檢查參數(shù),但只能檢查參數(shù)是bean的情況,對于參數(shù)是String或者Long類型的就不適用了,而且有時候這兩個注解又突然失效了(沒有仔細去調查過原因),對此,可以利用Spring的AOP和自定義注解,自己寫一個參數(shù)校驗的功能。
代碼示例
注意:本節(jié)代碼只是一個演示,給出一個可行的思路,并非完整的解決方案。
本項目是一個簡單Web項目,使用到了:Spring、SpringMVC、Maven、JDK1.8
項目結構:

自定義注解:
ValidParam.java:
package com.lzumetal.ssm.paramcheck.annotation;
import java.lang.annotation.*;
/**
* 標注在參數(shù)bean上,表示需要對該參數(shù)校驗
*/
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ValidParam {
}
NotNull.java:
package com.lzumetal.ssm.paramcheck.annotation;
import java.lang.annotation.*;
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NotNull {
String msg() default "字段不能為空";
}
NotEmpty.java:
package com.lzumetal.ssm.paramcheck.annotation;
import java.lang.annotation.*;
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NotEmpty {
String msg() default "字段不能為空";
}
切面類
ParamCheckAspect.java:
package com.lzumetal.ssm.paramcheck.aspect;
import com.lzumetal.ssm.paramcheck.annotation.NotEmpty;
import com.lzumetal.ssm.paramcheck.annotation.NotNull;
import com.lzumetal.ssm.paramcheck.annotation.ValidParam;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Field;
import java.lang.reflect.Parameter;
import java.util.Arrays;
/**
* 參數(shù)檢查切面類
*/
@Aspect
@Component
public class ParamCheckAspect {
@Before("execution(* com.lzumetal.ssm.paramcheck.controller.*.*(..))")
public void paramCheck(JoinPoint joinPoint) throws Exception {
//獲取參數(shù)對象
Object[] args = joinPoint.getArgs();
//獲取方法參數(shù)
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Parameter[] parameters = signature.getMethod().getParameters();
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
//Java自帶基本類型的參數(shù)(例如Integer、String)的處理方式
if (isPrimite(parameter.getType())) {
NotNull notNull = parameter.getAnnotation(NotNull.class);
if (notNull != null && args[i] == null) {
throw new RuntimeException(parameter.toString() + notNull.msg());
}
//TODO
continue;
}
/*
* 沒有標注@ValidParam注解,或者是HttpServletRequest、HttpServletResponse、HttpSession時,都不做處理
*/
if (parameter.getType().isAssignableFrom(HttpServletRequest.class) || parameter.getType().isAssignableFrom(HttpSession.class) ||
parameter.getType().isAssignableFrom(HttpServletResponse.class) || parameter.getAnnotation(ValidParam.class) == null) {
continue;
}
Class<?> paramClazz = parameter.getType();
//獲取類型所對應的參數(shù)對象,實際項目中Controller中的接口不會傳兩個相同的自定義類型的參數(shù),所以此處直接使用findFirst()
Object arg = Arrays.stream(args).filter(ar -> paramClazz.isAssignableFrom(ar.getClass())).findFirst().get();
//得到參數(shù)的所有成員變量
Field[] declaredFields = paramClazz.getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
//校驗標有@NotNull注解的字段
NotNull notNull = field.getAnnotation(NotNull.class);
if (notNull != null) {
Object fieldValue = field.get(arg);
if (fieldValue == null) {
throw new RuntimeException(field.getName() + notNull.msg());
}
}
//校驗標有@NotEmpty注解的字段,NotEmpty只用在String類型上
NotEmpty notEmpty = field.getAnnotation(NotEmpty.class);
if (notEmpty != null) {
if (!String.class.isAssignableFrom(field.getType())) {
throw new RuntimeException("NotEmpty Annotation using in a wrong field class");
}
String fieldStr = (String) field.get(arg);
if (StringUtils.isBlank(fieldStr)) {
throw new RuntimeException(field.getName() + notEmpty.msg());
}
}
}
}
}
/**
* 判斷是否為基本類型:包括String
* @param clazz clazz
* @return true:是; false:不是
*/
private boolean isPrimite(Class<?> clazz){
return clazz.isPrimitive() || clazz == String.class;
}
}
參數(shù)JavaBean
StudentParam.java:
package com.lzumetal.ssm.paramcheck.requestParam;
import com.lzumetal.ssm.paramcheck.annotation.NotEmpty;
import com.lzumetal.ssm.paramcheck.annotation.NotNull;
public class StudentParam {
@NotNull
private Integer id;
private Integer age;
@NotEmpty
private String name;
//get、set方法省略...
}
驗證參數(shù)校驗的Controller
TestController.java:
package com.lzumetal.ssm.paramcheck.controller;
import com.google.gson.Gson;
import com.lzumetal.ssm.paramcheck.annotation.NotNull;
import com.lzumetal.ssm.paramcheck.annotation.ValidParam;
import com.lzumetal.ssm.paramcheck.requestParam.StudentParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
private static Gson gson = new Gson();
@ResponseBody
@RequestMapping(value = "/test", method = RequestMethod.POST)
public StudentParam checkParam(@ValidParam StudentParam param, @NotNull Integer limit) {
System.out.println(gson.toJson(param));
System.out.println(limit);
return param;
}
}
本節(jié)示例代碼已上傳至GitHub:https://github.com/liaosilzu2007/ssm-parent.git
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解使用Mybatis-plus + velocity模板生成自定義的代碼
這篇文章主要介紹了詳解使用Mybatis-plus + velocity模板生成自定義的代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
Java?MethodHandles介紹與反射對比區(qū)別詳解
這篇文章主要為大家介紹了Java?MethodHandles介紹與反射對比區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
EasyExcel自定義下拉注解的三種實現(xiàn)方式總結
使用EasyExcel設置下拉數(shù)據(jù)時,每次都要創(chuàng)建一個SheetWriteHandler組件確實比較繁瑣,為了優(yōu)化這個過程,我們可以通過自定義注解來簡化操作,下面就來看看具體實現(xiàn)方法吧2024-10-10
解決SpringBoot中MultipartResolver和ServletFileUpload的沖突問題
這篇文章主要介紹了解決SpringBoot中MultipartResolver和ServletFileUpload的沖突問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
關于SpringGateway調用服務 接受不到參數(shù)問題
這篇文章主要介紹了關于SpringGateway調用服務接受不到參數(shù)問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
解決SpringBoot ClassPathResource的大坑(FileNotFoundException)
這篇文章主要介紹了解決SpringBoot ClassPathResource的大坑(FileNotFoundException),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Springboot結合JDBC實現(xiàn)雙數(shù)據(jù)源實例
這篇文章主要為大家介紹了Springboot結合JDBC實現(xiàn)雙數(shù)據(jù)源實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Java CountDownLatch計數(shù)器與CyclicBarrier循環(huán)屏障
CountDownLatch是一種同步輔助,允許一個或多個線程等待其他線程中正在執(zhí)行的操作的ASET完成。它允許一組線程同時等待到達一個共同的障礙點2023-04-04

