BizExceptionHandler.java 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package easydo.technology.config.exception;
  2. import lombok.extern.log4j.Log4j2;
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.http.ResponseEntity;
  5. import org.springframework.validation.BindException;
  6. import org.springframework.web.bind.annotation.ExceptionHandler;
  7. import org.springframework.web.bind.annotation.RestControllerAdvice;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. /**
  11. * 处理Controller抛出的异常*
  12. * @author tianyf
  13. */
  14. @RestControllerAdvice
  15. @Log4j2
  16. public class BizExceptionHandler {
  17. /**
  18. * 为 @NotNull / @NotEmpty / @NotBlank 提供异常后的消息抛出*
  19. * @param bindException
  20. * @return
  21. */
  22. @ExceptionHandler({BindException.class})
  23. public ResponseEntity<Object> throwCustomException(BindException bindException){
  24. log.error("[ @Vaild异常捕获 ] " + bindException.getMessage());
  25. Map<String, Object> vo = new HashMap<>();
  26. vo.put("message", bindException.getBindingResult().getFieldError().getDefaultMessage());
  27. vo.put("code", HttpStatus.BAD_REQUEST.value());
  28. return new ResponseEntity(vo, HttpStatus.OK);
  29. }
  30. }