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

詳解Spring AOP 實現(xiàn)“切面式”valid校驗

 更新時間:2018年01月17日 11:51:00   作者:CatalpaFlat  
本篇文章主要介紹了詳解Spring AOP 實現(xiàn)“切面式”valid校驗,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

why:

為什么要用aop實現(xiàn)校驗?

answer:

spring mvc 默認自帶的校驗機制 @Valid + BindingResult, 但這種默認實現(xiàn)都得在Controller方法的中去接收BindingResult,從而進行校驗.

eg:

if (result.hasErrors()) {
 List<ObjectError> allErrors = result.getAllErrors();
 List<String> errorlists = new ArrayList<>();
  for (ObjectError objectError : allErrors) {
    errorlists.add(objectError.getDefaultMessage());
  }
 }

獲取errorlists。這樣實現(xiàn)的話,每個需要校驗的方法都得重復(fù)調(diào)用,即使封裝也是。

可能上面那么說還不能表明spring 的@Valid + BindingResult實現(xiàn),我先舉個“栗子”。

1. 栗子(舊版本)

1.1 接口層(IDAL)

eg: 簡單的POST請求,@RequestBody接收請求數(shù)據(jù),@Valid + BindingResult進行校驗

  1. httpMethid: POST
  2. parameters:@RequestBody接收請求數(shù)據(jù)
  3. valid:@Valid +BindingResult
@ResponseBody
 @PostMapping("body")
 public ResponseVO bodyPost(@RequestBody @Valid TestVO body,BindingResult result){
  //校驗到錯誤
  if (result.hasErrors()) {
   List<ObjectError> allErrors = result.getAllErrors();
   List<String> lists = new ArrayList<>();
   for (ObjectError objectError : allErrors) {
     lists.add(objectError.getDefaultMessage());
   }
   return new ResponseVO(HttpStatus.BAD_REQUEST.value(), "parameter empty", lists);
 }
   return new ResponseVO(HttpStatus.OK.value(), "bodyPost", null);
}

1.2 實體(vo)校驗內(nèi)容

@Valid + BindingResult的校驗注解一大堆,網(wǎng)上一摸就有的!

public class TestVO {
  @Getter
  @Setter
  @Min(value = 0,message = "請求參數(shù)isString不能小于0")
  private Integer isInt;
  @Getter
  @Setter
  @NotBlank(message = "請求參數(shù)isString不能為空")
  private String isString;
}

1.3 結(jié)果測試

2. aop校驗(升級版)

可以看到若是多個像bodyPost一樣都需要對body進行校驗的話,那么有一坨代碼就必須不斷復(fù)現(xiàn),即使改為父類可復(fù)用方法,也得去調(diào)用。所以左思右想還是覺得不優(yōu)雅。所以有了aop進行切面校驗。

2.1 接口層(IDAL)

是的!你沒看錯,上面那一坨代碼沒了,也不需要調(diào)用父類的的共用方法。就單單一個注解就完事了:@ParamValid

@ParamValid
@ResponseBody
@PostMapping("body")
public ResponseVO bodyPost(@RequestBody @Valid TestVO body,BindingResult result){
  return new ResponseVO("bodyPost", null);
}

2.2 自定義注解(annotation)

這個注解也是簡簡單單的用于方法的注解。

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ParamValid {}

2.3 重點!切面實現(xiàn)(Aspect)

切面詳解:

@Before: 使用注解方式@annotation(XX),凡是使用到所需切的注解(@ParamValid),都會調(diào)用該方法

JoinPoint: 通過JoinPoint獲取方法的參數(shù),以此獲取BindingResult所校驗到的內(nèi)容

遷移校驗封裝: 將原先那一坨校驗遷移到Aspect中:validRequestParams

響應(yīng)校驗結(jié)果:

  1. 通過RequestContextHolder獲取response
  2. 獲取響應(yīng)OutputStream
  3. 將BindingResult封裝響應(yīng)
@Aspect
@Component
public class ParamValidAspect {

  private static final Logger log = LoggerFactory.getLogger(ParamValidAspect.class);

  @Before("@annotation(paramValid)")
  public void paramValid(JoinPoint point, ParamValid paramValid) {
    Object[] paramObj = point.getArgs();
    if (paramObj.length > 0) {
      if (paramObj[1] instanceof BindingResult) {
        BindingResult result = (BindingResult) paramObj[1];
        ResponseVO errorMap = this.validRequestParams(result);
        if (errorMap != null) {
          ServletRequestAttributes res = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
          HttpServletResponse response = res.getResponse();
          response.setCharacterEncoding("UTF-8");
          response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
          response.setStatus(HttpStatus.BAD_REQUEST.value());

          OutputStream output = null;
          try {
            output = response.getOutputStream();
            errorMap.setCode(null);
            String error = new Gson().toJson(errorMap);
            log.info("aop 檢測到參數(shù)不規(guī)范" + error);
            output.write(error.getBytes("UTF-8"));
          } catch (IOException e) {
            log.error(e.getMessage());
          } finally {
            try {
              if (output != null) {
                output.close();
              }
            } catch (IOException e) {
              log.error(e.getMessage());
            }
          }
        }
      }
    }
  }

  /**
   * 校驗
   */
  private ResponseVO validRequestParams(BindingResult result) {
    if (result.hasErrors()) {
      List<ObjectError> allErrors = result.getAllErrors();
      List<String> lists = new ArrayList<>();
      for (ObjectError objectError : allErrors) {
        lists.add(objectError.getDefaultMessage());
      }
      return new ResponseVO(HttpStatus.BAD_REQUEST.value(), "parameter empty", lists);
    }
    return null;
  }
}

2.4 測試結(jié)果

 

看了上面兩種結(jié)果,就可以對比出使用Spring AOP 配合@Valid + BindingResult進行校驗的優(yōu)點:

  1. 去除代碼冗余
  2. AOP異步處理
  3. 優(yōu)化代碼實現(xiàn)

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論