欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot中自定義注解實(shí)現(xiàn)參數(shù)非空校驗(yàn)的示例

 更新時(shí)間:2020年11月12日 14:38:51   作者:Sanarous  
這篇文章主要介紹了SpringBoot中自定義注解實(shí)現(xiàn)參數(shù)非空校驗(yàn),幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下

前言

由于剛寫項(xiàng)目不久,在寫 web 后臺接口時(shí),經(jīng)常會對前端傳入的參數(shù)進(jìn)行一些規(guī)則校驗(yàn),如果入?yún)⑤^少還好,一旦需要校驗(yàn)的參數(shù)比較多,那么使用 if 校驗(yàn)會帶來大量的重復(fù)性工作,并且代碼看起來會非常冗余,所以我首先想到能否通過一些手段改進(jìn)這點(diǎn),讓 Controller 層減少參數(shù)校驗(yàn)的冗余代碼,提升代碼的可閱讀性。

經(jīng)過閱讀他人的代碼,發(fā)現(xiàn)使用 annotation 注解是一個(gè)比較方便的手段,SpringBoot 自帶的 @RequestParam 注解只會校驗(yàn)請求中該參數(shù)是否存在,但是該參數(shù)是否符合一些規(guī)格比如不為 null 且不為空就無法進(jìn)行判斷的,所以我們可以嘗試一下增強(qiáng)請求參數(shù)中的注解。

準(zhǔn)備工作

有了前面的思路,我們先搭一個(gè)架子出來。

  • SpringBoot 2.3.5.REALEASE
  • JDK 1.8

pom.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è)注解,對必填的參數(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.自定義異常類

我們在這里自定義異常的原因,是為了配合自定義注解使用,一旦校驗(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)?code> 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
@Aspect
public 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 對象
  * @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)容(可以用來對處理返回值做一些加工處理)
  *
  * @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 請求
  * @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)行測試:

@RestController
public class HelloController {
 /**
  * 測試@RequestParam注解
  *
  * @param name 測試參數(shù)
  * @return 包裝結(jié)果
  */
 @GetMapping("/hello1")
 public Result<String> hello1(@RequestParam String name) {
  return ResponseMsgUtil.builderResponse(EnumResultCode.SUCCESS.getCode(), "請求成功", "Hello," + name);
 }

 /**
  * 測試@ParamCheck注解
  *
  * @param name 測試參數(shù)
  * @return 包裝結(jié)果
  */
 @GetMapping("/hello2")
 public Result<String> hello2(@ParamCheck String name) {
  return ResponseMsgUtil.builderResponse(EnumResultCode.SUCCESS.getCode(), "請求成功", "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(), "請求成功", "Hello," + name);
 }
}

測試訪問 http://localhost:8080/hello1,此時(shí)只有 @RequestParam 注解,如果不加 name 參數(shù),會請求得到一個(gè)異常:

并且控制臺會報(bào) MissingServletRequestParameterException: Required String parameter 'name' is not present] 異常

如果訪問 http://localhost:8080/hello2?name=,此時(shí)使用的是我們自定義的 @ParamCheck 注解,此時(shí)沒有參數(shù)輸入,那么也會捕獲輸入的異常:

如果訪問 http://localhost:8080/hello3?name=,此時(shí)既有參數(shù)存在校驗(yàn),又有我們自定義的 ParamCheck 不為空校驗(yàn),所以此時(shí)訪問不加參數(shù)會拋出異常:

控制臺拋出我們自定義的異常:

測試總結(jié):

當(dāng)參數(shù)名為空時(shí),分別添加兩個(gè)注解的接口都會提示參數(shù)不能為空
當(dāng)參數(shù)名不為空,值為空時(shí),@RequestParam注解不會報(bào)錯(cuò),但@ParamCheck注解提示參數(shù)'name'的值為空

6.總結(jié)

  • 經(jīng)過以上的測試也驗(yàn)證了 @RequestParam 只會驗(yàn)證對應(yīng)的參數(shù)是否存在,而不會驗(yàn)證值是否為空
  • ParamCheck 還可以進(jìn)行拓展,比如參數(shù)值長度、是否含有非法字符等校驗(yàn)

7.代碼附錄

上述使用到的代碼:

package cn.bestzuo.springbootannotation.common;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public 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ù)消息碼等生成接口返回對象
  *
  * @param code 結(jié)果返回碼
  * @param msg 結(jié)果返回消息
  * @param data 數(shù)據(jù)對象
  * @param <T> 泛型
  * @return 包裝對象
  */
 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;
 }

 /**
  * 請求異常返回結(jié)果
  *
  * @param <T> 泛型
  * @return 包裝對象
  */
 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)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring和Mybatis整合的原理詳解

    Spring和Mybatis整合的原理詳解

    這篇文章主要介紹了Spring和Mybatis整合的原理詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • Win10 Java jdk14.0.2安裝及環(huán)境變量配置詳細(xì)教程

    Win10 Java jdk14.0.2安裝及環(huán)境變量配置詳細(xì)教程

    這篇文章主要介紹了Win10 Java jdk14.0.2安裝及環(huán)境變量配置,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • SpringBoot+Thymeleaf+ECharts實(shí)現(xiàn)大數(shù)據(jù)可視化(基礎(chǔ)篇)

    SpringBoot+Thymeleaf+ECharts實(shí)現(xiàn)大數(shù)據(jù)可視化(基礎(chǔ)篇)

    本文主要介紹了SpringBoot+Thymeleaf+ECharts實(shí)現(xiàn)大數(shù)據(jù)可視化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2022-06-06
  • Java針對ArrayList自定義排序的2種實(shí)現(xiàn)方法

    Java針對ArrayList自定義排序的2種實(shí)現(xiàn)方法

    這篇文章主要介紹了Java針對ArrayList自定義排序的2種實(shí)現(xiàn)方法,結(jié)合實(shí)例形式總結(jié)分析了Java操作ArrayList自定義排序的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-01-01
  • java中volatile和synchronized的區(qū)別與聯(lián)系

    java中volatile和synchronized的區(qū)別與聯(lián)系

    這篇文章主要介紹了java中volatile和synchronized的區(qū)別與聯(lián)系的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • ArrayList在for循環(huán)中使用remove方法移除元素方法介紹

    ArrayList在for循環(huán)中使用remove方法移除元素方法介紹

    這篇文章主要介紹了ArrayList在for循環(huán)中使用remove方法移除元素的內(nèi)容,介紹了具體代碼實(shí)現(xiàn),需要的朋友可以參考下。
    2017-09-09
  • Druid簡單實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查方式

    Druid簡單實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查方式

    這篇文章主要介紹了Druid簡單實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Spring溫故而知新系列教程之AOP代理

    Spring溫故而知新系列教程之AOP代理

    Spring AOP 是代理模式的應(yīng)用,可以使用JDK提供的Proxy類或通過字節(jié)碼增強(qiáng)來實(shí)現(xiàn)。下面這篇文章主要給大家介紹了關(guān)于Spring之AOP代理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-05-05
  • 基于SpringBoot集成測試遠(yuǎn)程連接Redis服務(wù)的教程詳解

    基于SpringBoot集成測試遠(yuǎn)程連接Redis服務(wù)的教程詳解

    這篇文章主要介紹了基于SpringBoot集成測試遠(yuǎn)程連接的Redis服務(wù)的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Spring工作原理簡單探索

    Spring工作原理簡單探索

    這篇文章主要介紹了Spring工作原理簡單探索,涉及Springaop與IOC,動(dòng)態(tài)代理靜態(tài)代理,反射等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11

最新評論