zoukankan      html  css  js  c++  java
  • Eclipse搭建springboot项目(六)全局异常

    知识点:

      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

  • 相关阅读:
    十一.状态设计模式
    十. 享元设计模式
    Orcale(一)概念
    java类加载器
    spring中的事务管理机制
    spring中的annotation注解类配置
    countDownLatch和Semaphore用于多线程
    布隆过滤器
    mybatis-genator自动生成的mapper中模糊查询使用方法
    java中的异常
  • 原文地址:https://www.cnblogs.com/aaronRhythm/p/10961336.html
Copyright © 2011-2022 走看看