springboot学习笔记:全局捕获异常不生效

全局捕获异常,很明显的错误404返回给客户,很不好呀。整个web请求项目全局捕获异常,比如空指针直接返回给客户啊,那多操蛋呀~

 

看这几个常用的注解:

@ExceptionHandler 表示拦截异常

  • @ControllerAdvice 是 controller 的一个辅助类,最常用的就是作为全局异常处理的切面类
  • @ControllerAdvice 可以指定扫描范围
  • @ControllerAdvice 约定了几种可行的返回值,如果是直接返回 model 类的话,需要使用 @ResponseBody 进行 json 转换
  • 返回 String,表示跳到某个 view
  • 返回 modelAndView
  • 返回 model + @ResponseBody

创建meven工程。pom:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0modelVersion>

  <groupId>ErrorCatchgroupId>

  <artifactId>com.toov5.ErrorCatchartifactId>

  <version>0.0.1-SNAPSHOTversion>

   

  <parent>

        <groupId>org.springframework.bootgroupId>

        <artifactId>spring-boot-starter-parentartifactId>

        <version>2.0.0.RELEASEversion>

    parent>

    <dependencies>

        <dependency>

            <groupId>org.springframework.bootgroupId>

            <artifactId>spring-boot-starter-webartifactId>

        dependency>

    dependencies>

   

project>

 Java类

复制代码

package com.toov5.ErrorCatch;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
//以后经常做为服务异常捕获,要经常用到这个注解
public class ErrorCatch {
    @RequestMapping("/getUser")
    public String getUser(int i ){
        int j = 1/i;
        return "返回"+j;
        
    }
    
}

复制代码

项目目录结构:

springboot学习笔记:全局捕获异常不生效_第1张图片

启动,访问:

springboot学习笔记:全局捕获异常不生效_第2张图片

springboot学习笔记:全局捕获异常不生效_第3张图片

图2丑陋的页面出来了,用户体验很瞎眼(这算是个用户体验哈)~~

解决方案1: try catch 

复制代码

public class ErrorCatch {
    @RequestMapping("/getUser")
    public String getUser(int i ){
        int j = 0;
        try {
             j = 1/i;
            
        } catch (Exception e) {
            return "不好意思啊客户!~";
        }
        
        return "返回"+j;
    }
    
}

复制代码

springboot学习笔记:全局捕获异常不生效_第4张图片

如果每个错误,都要这样。。。。。那不蛋疼了啊~开发可要累死了

解决方案二 全局捕获异常   

 使用aop技术,采用异常通知

Spring Boot提供了支持,

首先建立一个类型,专门用来处理异常的

废话不多说,实战!

首先创建一个包 error(用来放异常捕获类的)

然后创建异常捕获类:

  1、返回json格式

  2、返回页面

首先创建类:

复制代码

package com.toov5.error;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice(basePackages="com.toov5.ErrorCatch")  //SpringBoot的异常切入点。此包下的包揽了哈
public class GlobalException {

   @ResponseBody  //返回jsoon给客户端
   public Map exceptionHandler(){
       Map map = new HashMap();
       map.put("errorCode", "666");
       map.put("errorMsg", "不好意思啊");
       return map;
       
   }
    
    
}

复制代码

 @ExceptionHandler(RuntimeException.class) 这个注解表示运行时异常

注意 原java类文件去掉try catch

复制代码

package com.toov5.ErrorCatch;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController //以后经常做微服务异常捕获,要经常用到这个注解
public class ErrorCatch {
    @RequestMapping("/getUser")
    public String getUser(int i ){
        int j = 1/i;    
        return "返回"+j;
    }
     
}

复制代码

小伙伴们注意了啊~~~

我在给大家写教程时候,发现了一个小问题,初学者们要注意了哦~!

@controlleradvice注解不起作用,Spring要扫描到!我这里的之前的目录结构不更改的前提下:

应该这样:给@SpringBootApplication添加扫包范围,之所以不生效的原因是因为没扫到全局异常处理类所在的包

springboot学习笔记:全局捕获异常不生效_第5张图片

一定要把 

GlobalException这个类扫到呀!!!

重启后,访问:

你可能感兴趣的:(springboot学习笔记:全局捕获异常不生效)