1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package com.tofly.scada.common;
- import com.tofly.common.core.entity.ResultRespone;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.core.annotation.Order;
- 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;
- @ControllerAdvice
- @Order(1)
- public class GlobalExceptionHandler {
- private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandler.class);
- @ResponseBody
- @ExceptionHandler(Exception.class)
- public ResultRespone exception(Exception e) {
- LOGGER.debug(e.getMessage());
- if (e instanceof MethodArgumentNotValidException) {
- MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
- String errMsg = ex.getBindingResult().getFieldError().getDefaultMessage();
- return ResultRespone.failed(errMsg);
- }
- if (e instanceof BindException){
- BindException ex = (BindException) e;
- String errMsg = ex.getBindingResult().getFieldError().getDefaultMessage();
- return ResultRespone.failed(errMsg);
- }
- return ResultRespone.failed(e.getMessage());
- }
- }
|