欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot全局异常处理详解

程序员文章站 2024-02-25 17:43:33
一、单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframe...

一、单个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;
  }

}

说明:

  1. 在controller中加入被@exceptionhandler修饰的类即可(在该注解中指定该方法需要处理的那些异常类)
  2. 该异常处理方法只在当前的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;
  }
}

说明:

  1. @controlleradvice是controller的一个辅助类,最常用的就是作为全局异常处理的切面类
  2. @controlleradvice可以指定扫描范围
  3. @controlleradvice约定了几种可行的返回值,如果是直接返回model类的话,需要使用@responsebody进行json转换
    1. 返回string,表示跳到某个view
    2. 返回modelandview
    3. 返回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;
  //  }

}

注意:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。