zoukankan      html  css  js  c++  java
  • SpringBoot学习(三)

    一、单个 controller 范围的异常处理

    复制代码
     package com.xxx.secondboot.web;
    
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.xxx.secondboot.exception.MyExceptionResponse;
    
    import io.swagger.annotations.Api;
    
    @Api("测试controllerAdvice和全局异常处理")
    @RestController
    @RequestMapping("/advice1")
    public class AdviceController {
    
        @RequestMapping(value = "/test1", method = RequestMethod.GET)
        public String test1() {
            throw new RuntimeException("advice1 - exception1");
        }
    
        @RequestMapping(value = "/test2", method = RequestMethod.GET)
        public String test2() {
            throw new RuntimeException("advice1 - exception2");
        }
    
        @ExceptionHandler(RuntimeException.class)
        public MyExceptionResponse exceptionHandler() {
            MyExceptionResponse resp = new MyExceptionResponse();
            resp.setCode(300);
            resp.setMsg("exception-Handler");
            return resp;
        }
    
    }
    复制代码

    说明:

    • 在 controller 中加入被 @ExceptionHandler 修饰的类即可(在该注解中指定该方法需要处理的那些异常类)
    • 该异常处理方法只在当前的 controller 中起作用

    二、全部 controller 范围内起作用的异常处理(全局异常处理)

    1、全局异常处理类

    复制代码
    package com.xxx.secondboot.web;
    
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.xxx.secondboot.exception.MyExceptionResponse;
    import com.xxx.secondboot.exception.MyRuntimeException;
    
    //@ControllerAdvice(annotations=RestController.class)
    //@ControllerAdvice(basePackages={"com.xxx","com.ooo"})
    @ControllerAdvice
    public class GlobalExceptionHandler {
        @ExceptionHandler(RuntimeException.class)
        //    @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class})
        //    @ExceptionHandler//处理所有异常
        @ResponseBody //在返回自定义相应类的情况下必须有,这是@ControllerAdvice注解的规定
        public MyExceptionResponse exceptionHandler(RuntimeException e, HttpServletResponse response) {
            MyExceptionResponse resp = new MyExceptionResponse();
            resp.setCode(300);
            resp.setMsg("exception-Handler");
            //        response.setStatus(600);
            return resp;
        }
    }
    复制代码

    说明:

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

    2、controller

    复制代码
    package com.xxx.secondboot.web;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import io.swagger.annotations.Api;
    
    @Api("测试controllerAdvice和全局异常处理")
    @RestController
    @RequestMapping("/advice1")
    public class AdviceController {
    
        @RequestMapping(value = "/test1", method = RequestMethod.GET)
        public String test1() {
            throw new RuntimeException("advice1 - exception1");
        }
    
        @RequestMapping(value = "/test2", method = RequestMethod.GET)
        public String test2() {
            throw new RuntimeException("advice1 - exception2");
        }
    
        //    @ExceptionHandler(RuntimeException.class)
        //    public MyExceptionResponse exceptionHandler() {
        //        MyExceptionResponse resp = new MyExceptionResponse();
        //        resp.setCode(300);
        //        resp.setMsg("exception-Handler");
        //        return resp;
        //    }
    
    }
    复制代码

    注意:

    • 同一个异常被局部范围异常处理器和全局范围异常处理器同时覆盖,会选择小范围的局部范围处理器
    • 同一个异常被小范围的异常类和大范围的异常处理器同时覆盖,会选择小范围的异常处理器

    参考自:

  • 相关阅读:
    let和const====均参考阮大神的es6入门
    面向对象封装
    记忆函数
    PHP面试题一[转]
    TDSSNIClient 初始化失败,出现错误 0x7e,状态代码 0x60。
    针对hasp加密狗服务器客户机ip不在同一个网段的解决方案
    sql 语句用isnull函数的用法
    如何用JS获取键盘上任意按键的值?兼容FireFox和IE js获取键盘ASCII码?js键盘事件全面控制
    adobe captivate 5.5 中文教程
    档案软件演示版下载安装说明书
  • 原文地址:https://www.cnblogs.com/yujihaia/p/7367579.html
Copyright © 2011-2022 走看看