<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Spring 是如何捕捉校驗(yàn)參數(shù)異常并處理的?

          共 2880字,需瀏覽 6分鐘

           ·

          2020-07-29 18:56

          出處:cnblogs.com/fqybzhangji/p/10384347.html


          使用?@Validated ,@Valid ,@NotBlank 之類的,請(qǐng)自行百度,本文著重于捕捉校驗(yàn)失敗信息并封裝返回出去


          參考:


          https://mp.weixin.qq.com/s/EaZxYKyC4L_EofWdtyBCpw

          https://www.jianshu.com/p/bcc5a3c86480


          捕捉校驗(yàn)失敗異常信息


          @ControllerAdvice
          public?class?WebExceptionHandler?{

          ???//處理Get請(qǐng)求中 使用@Valid 驗(yàn)證路徑中請(qǐng)求實(shí)體校驗(yàn)失敗后拋出的異常,詳情繼續(xù)往下看代碼
          ????@ExceptionHandler(BindException.class)
          ????@ResponseBody
          ????public?ResponseVO BindExceptionHandler(BindException e)?{
          ????????String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
          ????????return?new?ResponseVO(message);
          ????}

          ????//處理請(qǐng)求參數(shù)格式錯(cuò)誤 @RequestParam上validate失敗后拋出的異常是javax.validation.ConstraintViolationException
          ????@ExceptionHandler(ConstraintViolationException.class)
          ????@ResponseBody
          ????public?ResponseVO ConstraintViolationExceptionHandler(ConstraintViolationException e)?{
          ????????String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining());
          ????????return?new?ResponseVO(message);
          ????}

          ????//處理請(qǐng)求參數(shù)格式錯(cuò)誤 @RequestBody上validate失敗后拋出的異常是MethodArgumentNotValidException異常。
          ????@ExceptionHandler(MethodArgumentNotValidException.class)
          ????@ResponseBody
          ????public?ResponseVO MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e)?{
          ????????String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
          ????????return?new?ResponseVO(message);
          ????}
          }


          如下示例請(qǐng)求參數(shù)報(bào)錯(cuò)的話,會(huì)拋出?BindException 而不是??ConstraintViolationException 和?MethodArgumentNotValidException


          @RestController
          @RequestMapping("test")
          public class TestController {

          ????@GetMapping("refund")
          ????public ResponseVO refund(@Valid?RefundRequest request) throws Exception {
          ????????return?new?ResponseVO();
          ????}
          }

          @Data
          public?class?RefundRequest?implements?Serializable?{
          ????@NotBlank(message = "訂單號(hào)不能為空")
          ????private String orderId;
          ????@Min(value = 0, message = "已消費(fèi)金額金額不能為負(fù)數(shù)")
          ????private int orderAmt;
          }


          如果多個(gè)請(qǐng)求參數(shù)都校驗(yàn)失敗,則遇到第一個(gè)校驗(yàn)失敗就拋出異常,接下來的異常參數(shù)不做校驗(yàn),配置如下:


          @Configuration
          public?class?WebConfig?{
          ????@Bean
          ????public?Validator validator()?{
          ????????ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
          ????????????????.configure()
          ????????????????//failFast的意思只要出現(xiàn)校驗(yàn)失敗的情況,就立即結(jié)束校驗(yàn),不再進(jìn)行后續(xù)的校驗(yàn)。
          ????????????????.failFast(true)
          ????????????????.buildValidatorFactory();

          ????????return?validatorFactory.getValidator();
          ????}

          ????@Bean
          ????public?MethodValidationPostProcessor methodValidationPostProcessor()?{
          ????????MethodValidationPostProcessor methodValidationPostProcessor = new?MethodValidationPostProcessor();
          ????????methodValidationPostProcessor.setValidator(validator());
          ????????return?methodValidationPostProcessor;
          ????}
          }




          如果覺得不錯(cuò)點(diǎn)個(gè)“在看”喲!

          瀏覽 37
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲风情中文字幕 | 国内一级视频 | 日韩AV东京热一 | 草榴视频国产 | 疯狂而刺激3p视频 |