springboot异常总结

package com.ctsi.g5c.common.exception;

import com.ctsi.g5c.common.web.R;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

/**

 * @author shao
   */
   @RestControllerAdvice
   public class ExceptionAdvice {
   @ExceptionHandler(MethodArgumentNotValidException.class)
   @ResponseBody
   public R<Object> handleBindException(MethodArgumentNotValidException ex) {
       List<ObjectError> errors = ex.getBindingResult().getAllErrors();
       StringBuilder builder = new StringBuilder("[");
       errors.forEach(error -> {
           builder.append(error.getDefaultMessage());
       });
       return R.fail(builder.append("]").toString());
   }
   }

image-20210822175030988

image-20210822175011634

image-20210822175145072

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

spring.jackson.time-zone:GMT+8

image-20210822182957488

image-20210822183155747

posted @ 2021-09-10 11:33  泊月居  阅读(42)  评论(0编辑  收藏  举报