最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

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

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

準(zhǔn)備工作

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

  • 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 等.

使用自定義注解實現(xiàn)統(tǒng)一非空校驗

總體思路:自定義一個注解,對必填的參數(shù)加上該注解,然后定義一個切面,校驗該參數(shù)是否為空,如果為空則拋出自定義的異常,該異常被自定義的異常處理器捕獲,然后返回相應(yīng)的錯誤信息。

1.自定義注解

創(chuàng)建一個名為 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 {
 /**
  * 是否非空,默認不能為空
  */
 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)然,我們定義的注解可以擴展,不僅僅去校驗參數(shù)是否為空,比如我們可以增加字符串長度的校驗。

2.自定義異常類

我們在這里自定義異常的原因,是為了配合自定義注解使用,一旦校驗出不符合我們自定義注解規(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,并定義了兩個成員屬性、重寫了 getMessage() 方法
之所以自定義該異常,而不用現(xiàn)有的 org.springframework.web.bind.MissingServletRequestParameterException 類,是因為 MissingServletRequestParameterException為Checked 異常,在動態(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);

 /**
  * 定義有一個切入點,范圍為 controller 包下的類
  */
 @Pointcut("execution(public * cn.bestzuo.controller..*.*(..))")
 public void checkParam() {

 }

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

 /**
  * 檢查參數(shù)是否為空
  *
  * @param pjp 連接點
  * @return 對象
  * @throws Throwable 異常
  */
 @Around("checkParam()")
 public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
  MethodSignature signature = ((MethodSignature) pjp.getSignature());
  //得到攔截的方法
  Method method = signature.getMethod();
  //獲取方法參數(shù)注解,返回二維數(shù)組是因為某些參數(shù)可能存在多個注解
  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的實例,并且notNull()=true,則進行非空校驗
    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();
 }

 /**
  * 在切入點return內(nèi)容之后切入內(nèi)容(可以用來對處理返回值做一些加工處理)
  *
  * @param joinPoint 連接點
  */
 @AfterReturning("checkParam()")
 public void doAfterReturning(JoinPoint joinPoint) {
 }

 /**
  * 參數(shù)非空校驗,如果參數(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 異常,并進行處理,代碼如下:

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.測試

首先定義一個 Controller 進行測試:

@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一起時
  *
  * @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,此時只有 @RequestParam 注解,如果不加 name 參數(shù),會請求得到一個異常:

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

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

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

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

測試總結(jié):

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

6.總結(jié)

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

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中自定義注解實現(xiàn)參數(shù)非空校驗的示例的詳細內(nèi)容,更多關(guān)于SpringBoot 參數(shù)非空校驗的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 一篇文章帶你了解Java泛型的super和extends

    一篇文章帶你了解Java泛型的super和extends

    這篇文章主要介紹了Java泛型extends及super區(qū)別實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-08-08
  • Spring Data JPA的作用和用法小結(jié)

    Spring Data JPA的作用和用法小結(jié)

    Spring Data JPA是Spring框架的一個模塊,它提供了一種數(shù)據(jù)訪問抽象,允許以一種聲明式和簡潔的方式來處理數(shù)據(jù)庫操作,本文主要介紹了Spring Data JPA的作用和用法小結(jié),具有一定的參考價值,感興趣的可以了解一下
    2024-05-05
  • 并發(fā)編程之Java內(nèi)存模型順序一致性

    并發(fā)編程之Java內(nèi)存模型順序一致性

    這篇文章主要介紹了并發(fā)編程Java內(nèi)存模型順序一致性,順序一致性內(nèi)存模型是一個理論參考模型,處理器的內(nèi)存模型和編程語言的內(nèi)存模型都會以順序一致性內(nèi)存模型作為參照,下面我們一起進入文章看看學(xué)校內(nèi)容,需要的朋友可以參考一下
    2021-11-11
  • JAVA基于PDF box將PDF轉(zhuǎn)為圖片的實現(xiàn)方法

    JAVA基于PDF box將PDF轉(zhuǎn)為圖片的實現(xiàn)方法

    這篇文章主要介紹了JAVA基于PDF box將PDF轉(zhuǎn)為圖片的操作方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-07-07
  • springBoot整合shiro如何解決讀取不到@value值問題

    springBoot整合shiro如何解決讀取不到@value值問題

    這篇文章主要介紹了springBoot整合shiro如何解決讀取不到@value值問題,具有很好的參考價值,希望對大家有所幫助,
    2023-08-08
  • mybatis-plus IdWorker生成的Id和返回給前臺的不一致的解決

    mybatis-plus IdWorker生成的Id和返回給前臺的不一致的解決

    這篇文章主要介紹了mybatis-plus IdWorker生成的Id和返回給前臺的不一致的解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Spring mvc整合tiles框架的簡單入門教程(maven)

    Spring mvc整合tiles框架的簡單入門教程(maven)

    這篇文章主要給大家介紹了關(guān)于Spring mvc整合tiles框架的簡單入門教程(maven),文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考借鑒,下面來一起看看詳細的介紹吧。
    2017-12-12
  • java中int值直接賦值給char類型的方法詳解

    java中int值直接賦值給char類型的方法詳解

    這篇文章主要給大家介紹了關(guān)于java中int值直接賦值給char類型的相關(guān)資料,文中通過代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-12-12
  • Mybatis 查詢語句條件為枚舉類型時報錯的解決

    Mybatis 查詢語句條件為枚舉類型時報錯的解決

    這篇文章主要介紹了Mybatis 查詢語句條件為枚舉類型時報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • SpringBoot?多環(huán)境打包最佳實踐記錄

    SpringBoot?多環(huán)境打包最佳實踐記錄

    SpringBoot通過配置多環(huán)境文件和在打包時指定激活的環(huán)境,實現(xiàn)多環(huán)境打包與部署,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-11-11

最新評論

驻马店市| 依安县| 阿拉善盟| 含山县| 福鼎市| 武安市| 和田县| 连城县| 锡林郭勒盟| 女性| 醴陵市| 北海市| 夏邑县| 磐石市| 利辛县| 舒城县| 汤阴县| 沙洋县| 丰都县| 宜昌市| 即墨市| 绵竹市| 原阳县| 乐业县| 达日县| 六枝特区| 开江县| 沂源县| 淮南市| 库尔勒市| 开江县| 新源县| 德清县| 贺州市| 安新县| 抚宁县| 临汾市| 隆回县| 宾川县| 铜陵市| 黑河市|