spring boot/mvc通过@RestControllerAdvice或者@ControllerAdvice配合@ExceptionHandler实现全局异常统一处理

    在spring web项目开发中,我们经常会遇到各种exception,这些exception根据业务或者场景不同抛出不同的信息和返回类型,有的exception需要返回json数据格式的错误,有的exception又需要跳转到某个错误页面。这里将会讲解常见的几个exception统一处理。


通过注解@RestControllerAdvice统一处理rest接口的异常信息。

注意@RestControllerAdvice注解一般在spring boot接口项目中使用。spring普通项目也支持不过需要高版本的spring才有@RestControllerAdvice注解标签。

Java代码:
import com.leftso.business.response.Result;
import com.leftso.system.exception.BusinessException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class RestExceptionHandler {

    @ExceptionHandler(value = { BusinessException.class })
    public Result business(BusinessException ex) {
        return Result.fail("业务异常:"+ex.getMessage());
    }

    @ExceptionHandler(value = { MethodArgumentNotValidException.class })
    public Result methodArgumentNotValidException(MethodArgumentNotValidException ex) {
        String field=ex.getBindingResult().getFieldError().getField().replace("parameter.","");
        String message=ex.getBindingResult().getFieldError().getDefaultMessage();
        return Result.fail("验证失败:字段["+field+"]"+message);
    }

    @ExceptionHandler(value = { Exception.class })
    public Result exception(Exception ex) {
        return Result.fail("系统异常:"+ex.getMessage());
    }
}

通过注解@ControllerAdvice统一处理异常

import com.leftso.common.http.result.ResultJson;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {BusinessException.class})
    @ResponseBody
    public ResultJson business(BusinessException ex) {
        return ResultJson.fail("业务异常:" + ex.getMessage());
    }


    @ExceptionHandler(value = {Exception.class})
    public ModelAndView exception(Exception ex, HttpServletRequest request, HttpServletResponse response) {
        String uri = request.getRequestURI();
        ModelAndView modelAndView = new ModelAndView();
        if (uri.contains("enterprise")) {
            MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
            modelAndView.setView(jsonView);
            modelAndView.addObject(ResultJson.fail("系统异常:" + ex.getMessage()));
            return modelAndView;
        } else {
            RedirectView redirectView = new RedirectView("/static/error/500.jsp");
            redirectView.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
            modelAndView.setView(redirectView);
            return modelAndView;
        }
    }
}
暂无评论