码迷,mamicode.com
首页 > 编程语言 > 详细

Eclipse搭建springboot项目(六)全局异常

时间:2019-06-01 23:18:08      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:exception   clip   frame   art   tco   resource   rest   新建   framework   

知识点:

  1、SpringBoot2.x服务端异常和SpringBoot配置全局异常

    1)、默认异常测试 int i = 1/0,不友好
    2)、异常注解介绍
    @ControllerAdvice 如果是返回json数据 则用 RestControllerAdvice,就可以不加 @ResponseBody

    //捕获全局异常,处理所有不可知的异常
    @ExceptionHandler(value=Exception.class)

  2、SpringBoot2.x配置全局异常返回自定义异常和错误页面跳转

    1)、返回自定义异常界面,需要引入thymeleaf依赖
  

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

    2)、resource目录下新建templates,并新建error.html

ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("error.html");
modelAndView.addObject("msg", e.getMessage());
return modelAndView;

     https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-error-handling

Eclipse搭建springboot项目(六)全局异常

标签:exception   clip   frame   art   tco   resource   rest   新建   framework   

原文地址:https://www.cnblogs.com/aaronRhythm/p/10961336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!