SpringBoot中自定義注解實(shí)現(xiàn)參數(shù)非空校驗(yàn)的示例
前言
由于剛寫項(xiàng)目不久,在寫 web 后臺(tái)接口時(shí),經(jīng)常會(huì)對(duì)前端傳入的參數(shù)進(jìn)行一些規(guī)則校驗(yàn),如果入?yún)⑤^少還好,一旦需要校驗(yàn)的參數(shù)比較多,那么使用 if 校驗(yàn)會(huì)帶來大量的重復(fù)性工作,并且代碼看起來會(huì)非常冗余,所以我首先想到能否通過一些手段改進(jìn)這點(diǎn),讓 Controller 層減少參數(shù)校驗(yàn)的冗余代碼,提升代碼的可閱讀性。
經(jīng)過閱讀他人的代碼,發(fā)現(xiàn)使用 annotation 注解是一個(gè)比較方便的手段,SpringBoot 自帶的 @RequestParam 注解只會(huì)校驗(yàn)請(qǐng)求中該參數(shù)是否存在,但是該參數(shù)是否符合一些規(guī)格比如不為 null 且不為空就無法進(jìn)行判斷的,所以我們可以嘗試一下增強(qiáng)請(qǐng)求參數(shù)中的注解。
準(zhǔn)備工作
有了前面的思路,我們先搭一個(gè)架子出來。
SpringBoot 2.3.5.REALEASE JDK 1.8pom.xml 文件如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.bestzuo</groupId> <artifactId>springboot-annotation</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-annotation</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--引入AOP相應(yīng)的注解--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
其中 aspectjweaver 用于引入 AOP 的相關(guān)的注解,如 @Aspect、@Pointcut 等.
使用自定義注解實(shí)現(xiàn)統(tǒng)一非空校驗(yàn)
總體思路:自定義一個(gè)注解,對(duì)必填的參數(shù)加上該注解,然后定義一個(gè)切面,校驗(yàn)該參數(shù)是否為空,如果為空則拋出自定義的異常,該異常被自定義的異常處理器捕獲,然后返回相應(yīng)的錯(cuò)誤信息。
1.自定義注解
創(chuàng)建一個(gè)名為 ParamCheck 的注解,代碼如下:
package cn.bestzuo.springbootannotation.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 參數(shù)不能為空注解,作用于方法參數(shù)上 * * @author zuoxiang * @since 2020-11-11 */@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)public @interface ParamCheck { /** * 是否非空,默認(rèn)不能為空 */ boolean notNull() default true;}
其中 @Target 注解中的 ElementType.PARAMETER 表示該注解的作用范圍,我們查看源碼可以看到,注解的作用范圍定義比較廣泛,可以作用于方法、參數(shù)、構(gòu)造方法、本地變量、枚舉等等。
public enum ElementType { /** Class, interface (including annotation type), or enum declaration */ TYPE, /** Field declaration (includes enum constants) */ FIELD, /** Method declaration */ METHOD, /** Formal parameter declaration */ PARAMETER, /** Constructor declaration */ CONSTRUCTOR, /** Local variable declaration */ LOCAL_VARIABLE, /** Annotation type declaration */ ANNOTATION_TYPE, /** Package declaration */ PACKAGE, /** * Type parameter declaration * * @since 1.8 */ TYPE_PARAMETER, /** * Use of a type * * @since 1.8 */ TYPE_USE}
當(dāng)然,我們定義的注解可以擴(kuò)展,不僅僅去校驗(yàn)參數(shù)是否為空,比如我們可以增加字符串長度的校驗(yàn)。
2.自定義異常類
我們?cè)谶@里自定義異常的原因,是為了配合自定義注解使用,一旦校驗(yàn)出不符合我們自定義注解規(guī)格的參數(shù),可以直接拋出自定義異常返回。代碼如下:
package cn.bestzuo.springbootannotation.exception;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; } /** * 重寫了該方法 * * @return 異常消息通知 */ @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,并定義了兩個(gè)成員屬性、重寫了 getMessage() 方法之所以自定義該異常,而不用現(xiàn)有的 org.springframework.web.bind.MissingServletRequestParameterException 類,是因?yàn)?MissingServletRequestParameterException為Checked 異常,在動(dòng)態(tài)代理過程中,很容易引發(fā) java.lang.reflect.UndeclaredThrowableException 異常。
3.自定義 AOP
代碼如下:
package cn.bestzuo.springbootannotation.aop;import cn.bestzuo.springbootannotation.annotation.ParamCheck;import cn.bestzuo.springbootannotation.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;@Component@Aspectpublic class ParamCheckAop { private static final Logger LOGGER = LoggerFactory.getLogger(ParamCheckAop.class); /** * 定義有一個(gè)切入點(diǎn),范圍為 controller 包下的類 */ @Pointcut('execution(public * cn.bestzuo.controller..*.*(..))') public void checkParam() { } @Before('checkParam()') public void doBefore(JoinPoint joinPoint) { } /** * 檢查參數(shù)是否為空 * * @param pjp 連接點(diǎn) * @return 對(duì)象 * @throws Throwable 異常 */ @Around('checkParam()') public Object doAround(ProceedingJoinPoint pjp) throws Throwable { MethodSignature signature = ((MethodSignature) pjp.getSignature()); //得到攔截的方法 Method method = signature.getMethod(); //獲取方法參數(shù)注解,返回二維數(shù)組是因?yàn)槟承﹨?shù)可能存在多個(gè)注解 Annotation[][] parameterAnnotations = method.getParameterAnnotations(); if (parameterAnnotations.length == 0) { return pjp.proceed(); } //獲取方法參數(shù)名 String[] paramNames = signature.getParameterNames(); //獲取參數(shù)值 Object[] paramValues = pjp.getArgs(); //獲取方法參數(shù)類型 Class<?>[] parameterTypes = method.getParameterTypes(); for (int i = 0; i < parameterAnnotations.length; i++) { for (int j = 0; j < parameterAnnotations[i].length; j++) { //如果該參數(shù)前面的注解是ParamCheck的實(shí)例,并且notNull()=true,則進(jìn)行非空校驗(yàn) if (parameterAnnotations[i][j] != null && parameterAnnotations[i][j] instanceof ParamCheck && ((ParamCheck) parameterAnnotations[i][j]).notNull()) { paramIsNull(paramNames[i], paramValues[i], parameterTypes[i] == null ? null : parameterTypes[i].getName()); break; } } } return pjp.proceed(); } /** * 在切入點(diǎn)return內(nèi)容之后切入內(nèi)容(可以用來對(duì)處理返回值做一些加工處理) * * @param joinPoint 連接點(diǎn) */ @AfterReturning('checkParam()') public void doAfterReturning(JoinPoint joinPoint) { } /** * 參數(shù)非空校驗(yàn),如果參數(shù)為空,則拋出ParamIsNullException異常 * * @param paramName 參數(shù)名稱 * @param value 參數(shù)值 * @param parameterType 參數(shù)類型 */ private void paramIsNull(String paramName, Object value, String parameterType) { if (value == null || ''.equals(value.toString().trim())) { throw new ParamIsNullException(paramName, parameterType); } }}
4.全局異常處理器
該異常處理器捕獲在 ParamCheckAop 類中拋出的 ParamIsNullException 異常,并進(jìn)行處理,代碼如下:
import cn.bestzuo.springbootannotation.common.Result;import cn.bestzuo.springbootannotation.enums.EnumResultCode;import cn.bestzuo.springbootannotation.utils.ResponseMsgUtil;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.MissingServletRequestParameterException;import org.springframework.web.bind.annotation.ExceptionHandler;import javax.servlet.http.HttpServletRequest;public class GlobalExceptionHandler { private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class); /** * 參數(shù)為空異常處理 * * @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 請(qǐng)求 * @param e 異常體 * @return 返回的異常 */ @ExceptionHandler(value = Exception.class) public Result<String> errorHandler(HttpServletRequest request, Exception e) { LOGGER.error('request Exception:', e); return ResponseMsgUtil.exception(); }}
5.測試
首先定義一個(gè) Controller 進(jìn)行測試:
@RestControllerpublic class HelloController { /** * 測試@RequestParam注解 * * @param name 測試參數(shù) * @return 包裝結(jié)果 */ @GetMapping('/hello1') public Result<String> hello1(@RequestParam String name) { return ResponseMsgUtil.builderResponse(EnumResultCode.SUCCESS.getCode(), '請(qǐng)求成功', 'Hello,' + name); } /** * 測試@ParamCheck注解 * * @param name 測試參數(shù) * @return 包裝結(jié)果 */ @GetMapping('/hello2') public Result<String> hello2(@ParamCheck String name) { return ResponseMsgUtil.builderResponse(EnumResultCode.SUCCESS.getCode(), '請(qǐng)求成功', 'Hello,' + name); } /** * 測試@ParamCheck與@RequestParam一起時(shí) * * @param name 測試參數(shù) * @return 包裝結(jié)果 */ @GetMapping('/hello3') public Result<String> hello3(@ParamCheck @RequestParam String name) { return ResponseMsgUtil.builderResponse(EnumResultCode.SUCCESS.getCode(), '請(qǐng)求成功', 'Hello,' + name); }}
測試訪問 http://localhost:8080/hello1,此時(shí)只有 @RequestParam 注解,如果不加 name 參數(shù),會(huì)請(qǐng)求得到一個(gè)異常:
并且控制臺(tái)會(huì)報(bào) MissingServletRequestParameterException: Required String parameter ’name’ is not present] 異常
如果訪問 http://localhost:8080/hello2?name=,此時(shí)使用的是我們自定義的 @ParamCheck 注解,此時(shí)沒有參數(shù)輸入,那么也會(huì)捕獲輸入的異常:
如果訪問 http://localhost:8080/hello3?name=,此時(shí)既有參數(shù)存在校驗(yàn),又有我們自定義的 ParamCheck 不為空校驗(yàn),所以此時(shí)訪問不加參數(shù)會(huì)拋出異常:
控制臺(tái)拋出我們自定義的異常:
測試總結(jié):
當(dāng)參數(shù)名為空時(shí),分別添加兩個(gè)注解的接口都會(huì)提示參數(shù)不能為空當(dāng)參數(shù)名不為空,值為空時(shí),@RequestParam注解不會(huì)報(bào)錯(cuò),但@ParamCheck注解提示參數(shù)’name’的值為空
6.總結(jié)
經(jīng)過以上的測試也驗(yàn)證了 @RequestParam 只會(huì)驗(yàn)證對(duì)應(yīng)的參數(shù)是否存在,而不會(huì)驗(yàn)證值是否為空 ParamCheck 還可以進(jìn)行拓展,比如參數(shù)值長度、是否含有非法字符等校驗(yàn)7.代碼附錄
上述使用到的代碼:
package cn.bestzuo.springbootannotation.common;import lombok.Getter;import lombok.Setter;@Getter@Setterpublic class Result<T> { private Integer resCode; private String resMsg; private T data;}
package cn.bestzuo.springbootannotation.enums;/** * 枚舉參數(shù)結(jié)果 * * @author zuoxiang * @since 2020-11-11 */public enum EnumResultCode { SUCCESS(200), FAIL(400), UNAUTHORIZED(401), NOT_FOUND(404), INTERNAL_SERVER_ERROR(500); private final int code; EnumResultCode(int code) { this.code = code; } public int getCode() { return code; }}
package cn.bestzuo.springbootannotation.utils;import cn.bestzuo.springbootannotation.common.Result;import cn.bestzuo.springbootannotation.enums.EnumResultCode;public class ResponseMsgUtil { /** * 根據(jù)消息碼等生成接口返回對(duì)象 * * @param code 結(jié)果返回碼 * @param msg 結(jié)果返回消息 * @param data 數(shù)據(jù)對(duì)象 * @param <T> 泛型 * @return 包裝對(duì)象 */ public static <T> Result<T> builderResponse(int code, String msg, T data) { Result<T> res = new Result<>(); res.setResCode(code); res.setResMsg(msg); res.setData(data); return res; } /** * 請(qǐng)求異常返回結(jié)果 * * @param <T> 泛型 * @return 包裝對(duì)象 */ public static <T> Result<T> exception() { return builderResponse(EnumResultCode.INTERNAL_SERVER_ERROR.getCode(), '服務(wù)異常', null); }}
以上就是SpringBoot中自定義注解實(shí)現(xiàn)參數(shù)非空校驗(yàn)的示例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 參數(shù)非空校驗(yàn)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 開發(fā)效率翻倍的Web API使用技巧2. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼3. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式4. 什么是Python變量作用域5. ASP常用日期格式化函數(shù) FormatDate()6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. java加載屬性配置properties文件的方法9. Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實(shí)現(xiàn)教程10. python 如何在 Matplotlib 中繪制垂直線
