[关闭]
@javazjm 2017-10-30T02:59:43.000000Z 字数 2616 阅读 2823

SpringBoot系列学习十:统一异常处理

Springboot 异常


BasicErrorController

SpringBoot内置了一个BasicErrorController对异常进行统一的处理,当在页面发生异常的时候会自动把请求转到/error(Spring Boot提供的一个默认的映射)
,可以自定义页面内容,只需在classpath路径下新建error页面即可。当然我们也可以自定义error页面的路径
如:

  1. server.error.path=/custom/error

BasicErrorController提供两种返回错误一种是页面返回、当你是页面请求的时候就会返回页面,另外一种是json请求的时候就会返回json错误

可以查看源码。

1. 定义全局异常处理类:并用@ControllerAdvice注解

  1. /**
  2. * 统一异常处理
  3. * Created by admin on 2017/8/7.
  4. */
  5. @ControllerAdvice
  6. public class MyExceptionHandler {
  7. /**
  8. * 异常处理, 返回视图
  9. *
  10. * @param req
  11. * @param e
  12. * @return
  13. * @throws Exception
  14. */
  15. @ExceptionHandler(value = Exception.class)
  16. public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
  17. ModelAndView modelAndView = new ModelAndView();
  18. modelAndView.addObject("exception", e);
  19. modelAndView.addObject("url", req.getRequestURL());
  20. modelAndView.setViewName("error");
  21. return modelAndView;
  22. }
  23. /**
  24. * 异常处理,返回自定义的异常对象json
  25. *
  26. * @param req
  27. * @param e
  28. * @return
  29. * @throws Exception
  30. */
  31. @ExceptionHandler(value = MyException.class)
  32. @ResponseBody
  33. public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, MyException e) throws Exception {
  34. ErrorInfo<String> r = new ErrorInfo<>();
  35. r.setMessage(e.getMessage());
  36. r.setCode(ErrorInfo.ERROR);
  37. r.setData("Some Data");
  38. r.setUrl(req.getRequestURL().toString());
  39. return r;
  40. }
  41. @ModelAttribute
  42. public Model newUser(Model model) {
  43. System.out.println("============应用到所有@RequestMapping注解方法,在其执行之前把返回值放入Model");
  44. model.addAttribute("user", "hong");
  45. return model;
  46. }
  47. @InitBinder
  48. public void initBinder(WebDataBinder binder) {
  49. System.out.println("============应用到所有@RequestMapping注解方法,在其执行之前初始化数据绑定器");
  50. }
  51. }

2. 定义异常信息类 ErrorInfo

  1. public class ErrorInfo<T> {
  2. public static final Integer OK = 0;
  3. public static final Integer ERROR = 500;
  4. private Integer code;
  5. private String message;
  6. private String url;
  7. private T data;
  8. public static Integer getERROR() {
  9. return ERROR;
  10. }
  11. public Integer getCode() {
  12. return code;
  13. }
  14. public void setCode(Integer code) {
  15. this.code = code;
  16. }
  17. public String getMessage() {
  18. return message;
  19. }
  20. public void setMessage(String message) {
  21. this.message = message;
  22. }
  23. public String getUrl() {
  24. return url;
  25. }
  26. public void setUrl(String url) {
  27. this.url = url;
  28. }
  29. public T getData() {
  30. return data;
  31. }
  32. public void setData(T data) {
  33. this.data = data;
  34. }
  35. }

3. 自定义异常类,继承Exception(或RuntimeException)

  1. public class MyException extends Exception{
  2. public MyException(String message) {
  3. super(message);
  4. }
  5. }

4. 页面

路径:src/main/resources/templates/error.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. 请求地址:
  9. <span th:text="${url}"></span>
  10. <p>发生了异常...</p>
  11. </body>
  12. </html>

参考:didi

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注