一、springboot Restful使用@ControllerAdvice、@ExceptionHandler、@ResponseBody实现全局异常处理
    @ControllerAdvice 注解定义全局异常处理类
    @ExceptionHandler 指定自定义错误处理方法拦截的异常类型

    同一个异常被小范围的异常类和大范围的异常处理器同时覆盖,会选择小范围的异常处理器
    1.定义异常业务类

    1. /**
    2. * 异常VO
    3. *
    4. * @date 2017年2月17日
    5. * @since 1.0.0
    6. */
    7. public class ExceptionVO {
    8. private String errorCode;
    9. private String message;
    10. public String getMessage() {
    11. return message;
    12. }
    13. public void setMessage(String message) {
    14. this.message = message;
    15. }
    16. public String getErrorCode() {
    17. return errorCode;
    18. }
    19. public void setErrorCode(String errorCode) {
    20. this.errorCode = errorCode;
    21. }
    22. }

    2.定义自定义异常

    1. package exception;
    2. /**
    3. * 无数据Exception
    4. *
    5. * @date 17/4/25
    6. * @since 1.0.0
    7. */
    8. public class NotFoundException extends SystemException {
    9. public NotFoundException(String message) {
    10. super(message);
    11. }
    12. }
    13. /**
    14. * 系统异常
    15. *
    16. * @date 2017年2月12日
    17. * @since 1.0.0
    18. */
    19. public class SystemException extends RuntimeException {
    20. private static final long serialVersionUID = 1095242212086237834L;
    21. protected Object errorCode;
    22. protected Object[] args;
    23. public SystemException() {
    24. super();
    25. }
    26. public SystemException(String message, Throwable cause) {
    27. super(message, cause);
    28. }
    29. public SystemException(String message) {
    30. super(message);
    31. }
    32. public SystemException(String message, Object[] args, Throwable cause) {
    33. super(message, cause);
    34. this.args = args;
    35. }
    36. public SystemException(String message, Object[] args) {
    37. super(message);
    38. this.args = args;
    39. }
    40. public SystemException(Object errorCode, String message, Throwable cause) {
    41. super(message, cause);
    42. this.errorCode = errorCode;
    43. }
    44. public SystemException(Object errorCode, String message) {
    45. super(message);
    46. this.errorCode = errorCode;
    47. }
    48. public SystemException(Object errorCode, String message, Object[] args, Throwable cause) {
    49. super(message, cause);
    50. this.args = args;
    51. this.errorCode = errorCode;
    52. }
    53. public SystemException(Object errorCode, String message, Object[] args) {
    54. super(message);
    55. this.args = args;
    56. this.errorCode = errorCode;
    57. }
    58. public SystemException(Throwable cause) {
    59. super(cause);
    60. }
    61. public Object[] getArgs() {
    62. return args;
    63. }
    64. public Object getErrorCode() {
    65. return errorCode;
    66. }
    67. }

    3.定义全局异常处理类

    1. import java.util.HashMap;
    2. import java.util.Map;
    3. import java.util.stream.Collectors;
    4. import NotFoundException;
    5. import org.apache.commons.collections.CollectionUtils;
    6. import org.slf4j.Logger;
    7. import org.slf4j.LoggerFactory;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.context.MessageSource;
    10. import org.springframework.context.NoSuchMessageException;
    11. import org.springframework.context.i18n.LocaleContextHolder;
    12. import org.springframework.http.HttpStatus;
    13. import org.springframework.validation.BindException;
    14. import org.springframework.validation.FieldError;
    15. import org.springframework.web.bind.annotation.ControllerAdvice;
    16. import org.springframework.web.bind.annotation.ExceptionHandler;
    17. import org.springframework.web.bind.annotation.ResponseBody;
    18. import org.springframework.web.bind.annotation.ResponseStatus;
    19. import ExceptionVO;
    20. /**
    21. * WEB异常处理器
    22. *
    23. * @date 2017年2月16日
    24. * @since 1.0.0
    25. */
    26. @ControllerAdvice("web") //指定异常处理期拦截范围
    27. public class WebExceptionHandler {
    28. static Logger LOG = LoggerFactory.getLogger(WebExceptionHandler.class);
    29. @Autowired
    30. private MessageSource messageSource;
    31. @ExceptionHandler(FieldException.class)
    32. @ResponseStatus(HttpStatus.CONFLICT) //指定http响应状态
    33. @ResponseBody
    34. /**
    35. * 未找到数据
    36. *
    37. * @param e
    38. * @return
    39. */
    40. @ExceptionHandler(NotFoundException.class)//指定异常类型
    41. @ResponseStatus(HttpStatus.NOT_FOUND)
    42. @ResponseBody
    43. public ExceptionVO handleNotFoundException(NotFoundException e) {
    44. ExceptionVO vo = new ExceptionVO();
    45. fillExceptionVO(e, vo);
    46. return vo;
    47. }
    48. @ExceptionHandler(SystemException.class)
    49. @ResponseStatus(HttpStatus.CONFLICT)
    50. @ResponseBody
    51. public ExceptionVO handleSystemException(SystemException e) {
    52. ExceptionVO vo = new ExceptionVO();
    53. fillExceptionVO(e, vo);
    54. return vo;
    55. }
    56. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    57. @ExceptionHandler(Exception.class)
    58. public void globalError(Exception e) {
    59. LOG.error(e.getMessage(), e);
    60. }
    61. /**
    62. * 填充异常响应消息
    63. *
    64. * @param e
    65. * @param vo
    66. */
    67. private void fillExceptionVO(SystemException e, ExceptionVO vo) {
    68. if (e.getMessage() != null) {
    69. String message = e.getMessage();
    70. try {
    71. message = messageSource.getMessage(e.getMessage(), e.getArgs(), LocaleContextHolder.getLocale());
    72. } catch (NoSuchMessageException ex) {
    73. ; // ignore
    74. }
    75. vo.setMessage(message);
    76. }
    77. vo.setErrorCode(String.valueOf(e.getErrorCode()));
    78. }
    79. }

    二、springboot 返回 ModelAndView

    1. package exception.handler;
    2. import javax.servlet.http.HttpServletRequest;
    3. import javax.servlet.http.HttpServletResponse;
    4. import org.springframework.web.servlet.HandlerExceptionResolver;
    5. import org.springframework.web.servlet.ModelAndView;
    6. @Commpent
    7. public class OverallExceptionHandler implements HandlerExceptionResolver {
    8. @Override
    9. public ModelAndView resolveException(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
    10. Exception ex) {
    11. ModelAndView mav = new ModelAndView();
    12. System.out.println(ex.getMessage());
    13. mav.addObject("errMsg", ex.getMessage());
    14. mav.setViewName("error");
    15. return mav;
    16. }
    17. }

    其它方式:

    1. @ControllerAdvice
    2. public class GlobalExceptionHandler {
    3. @ExceptionHandler(value = Exception.class)
    4. public ModelAndView resolveException(HttpServletRequest request, Exception ex) throws Exception {
    5. ModelAndView mav = new ModelAndView();
    6. System.out.println(ex.getMessage());
    7. mav.addObject("errMsg", ex.getMessage());
    8. mav.setViewName("error");
    9. return mav;
    10. }
    11. }