guoshilong
2023-09-26 c5320fbf7fd671ba7ccefe66e951edeebb4252b2
src/main/java/com/dji/sample/component/GlobalExceptionHandler.java
@@ -1,6 +1,8 @@
package com.dji.sample.component;
import com.dji.sample.common.model.ResponseResult;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -30,4 +32,11 @@
        e.printStackTrace();
        return ResponseResult.error("A null object appeared.");
    }
    @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
    public ResponseResult methodArgumentNotValidExceptionHandler(BindException e) {
        e.printStackTrace();
        return ResponseResult.error(e.getFieldError().getField() + e.getFieldError().getDefaultMessage());
    }
}