亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

why:

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

answer:

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

eg:

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

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

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

1. 栗子(舊版本)

1.1 接口層(IDAL)

eg: 簡(jiǎn)單的POST請(qǐng)求,@RequestBody接收請(qǐng)求數(shù)據(jù),@Valid + BindingResult進(jìn)行校驗(yàn)

  1. httpMethid: POST
  2. parameters:@RequestBody接收請(qǐng)求數(shù)據(jù)
  3. valid:@Valid +BindingResult
@ResponseBody
 @PostMapping("body")
 public ResponseVO bodyPost(@RequestBody @Valid TestVO body,BindingResult result){
  //校驗(yàn)到錯(cuò)誤
  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 實(shí)體(vo)校驗(yàn)內(nèi)容

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

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

1.3 結(jié)果測(cè)試

2. aop校驗(yàn)(升級(jí)版)

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

2.1 接口層(IDAL)

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

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

2.2 自定義注解(annotation)

這個(gè)注解也是簡(jiǎn)簡(jiǎn)單單的用于方法的注解。

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

2.3 重點(diǎn)!切面實(shí)現(xiàn)(Aspect)

切面詳解:

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

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

遷移校驗(yàn)封裝: 將原先那一坨校驗(yàn)遷移到Aspect中:validRequestParams

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

  1. 通過(guò)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 檢測(cè)到參數(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());
            }
          }
        }
      }
    }
  }

  /**
   * 校驗(yàn)
   */
  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 測(cè)試結(jié)果

 

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

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論