1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
@ControllerAdvice public class GlobalExceptionHandler {
@ExceptionHandler(value = NoHandlerFoundException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public ModelAndView errorHandler(HttpServletRequest request, NoHandlerFoundException exception, HttpServletResponse response) { return commonHandler(request, response, exception.getClass().getSimpleName(), HttpStatus.NOT_FOUND.value(), exception.getMessage()); }
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) public ModelAndView errorHandler(HttpServletRequest request, HttpRequestMethodNotSupportedException exception, HttpServletResponse response) { return commonHandler(request, response, exception.getClass().getSimpleName(), HttpStatus.METHOD_NOT_ALLOWED.value(), exception.getMessage()); }
@ExceptionHandler(HttpMediaTypeNotSupportedException.class) public ModelAndView errorHandler(HttpServletRequest request, HttpMediaTypeNotSupportedException exception, HttpServletResponse response) { return commonHandler(request, response, exception.getClass().getSimpleName(), HttpStatus.UNSUPPORTED_MEDIA_TYPE.value(), exception.getMessage()); }
@ExceptionHandler(value = Exception.class) public ModelAndView errorHandler (HttpServletRequest request, Exception exception, HttpServletResponse response) { return commonHandler(request, response, exception.getClass().getSimpleName(), HttpStatus.INTERNAL_SERVER_ERROR.value(), exception.getMessage()); }
@ExceptionHandler(value = BasicException.class) private ModelAndView errorHandler (HttpServletRequest request, BasicException exception, HttpServletResponse response) { return commonHandler(request, response, exception.getClass().getSimpleName(), exception.getCode(), exception.getMessage()); }
@ExceptionHandler(value = BindException.class) @ResponseBody public ExceptionEntity validExceptionHandler(BindException exception, HttpServletRequest request, HttpServletResponse response) { List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors(); Map<String,String> errors = new HashMap<>(); for (FieldError error:fieldErrors) { errors.put(error.getField(), error.getDefaultMessage()); } ExceptionEntity entity = new ExceptionEntity(); entity.setMessage(JSON.toJSONString(errors)); entity.setPath(request.getRequestURI()); entity.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value()); entity.setError(exception.getClass().getSimpleName()); response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); return entity; }
private ModelAndView commonHandler (HttpServletRequest request, HttpServletResponse response, String error, int httpCode, String message) { ExceptionEntity entity = new ExceptionEntity(); entity.setPath(request.getRequestURI()); entity.setError(error); entity.setCode(httpCode); entity.setMessage(message); return determineOutput(request, response, entity); }
private ModelAndView determineOutput(HttpServletRequest request, HttpServletResponse response, ExceptionEntity entity) { if (!( request.getHeader("accept").contains("application/json") || (request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").contains("XMLHttpRequest")) )) { ModelAndView modelAndView = new ModelAndView("error"); modelAndView.addObject("exception", entity); return modelAndView; } else { response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); response.setCharacterEncoding("UTF8"); response.setHeader("Content-Type", "application/json"); try { response.getWriter().write(ResultJsonTools.build( ResponseCodeConstant.SYSTEM_ERROR, ResponseMessageConstant.APP_EXCEPTION, JSONObject.parseObject(JSON.toJSONString(entity)) )); } catch (IOException e) { e.printStackTrace(); } return null; } } }
|