rain
2024-07-30 d81fdeef2ad2f7ecc548cebad6ab15455bf3da27
src/main/java/com/dji/sample/component/GlobalExceptionHandler.java
@@ -1,6 +1,7 @@
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;
@@ -29,12 +30,13 @@
    @ExceptionHandler(NullPointerException.class)
    public ResponseResult nullPointerExceptionHandler(NullPointerException e) {
        e.printStackTrace();
        return ResponseResult.error("A null object appeared.");
        return ResponseResult.error("出现空对象");
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        return ResponseResult.error(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
    public ResponseResult methodArgumentNotValidExceptionHandler(BindException e) {
        e.printStackTrace();
        return ResponseResult.error(e.getFieldError().getField() + e.getFieldError().getDefaultMessage());
    }
}