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

SpringBoot 统一异常处理详解

程序员文章站 2024-02-25 11:15:40
代码结构 配置pom文件

代码结构

SpringBoot 统一异常处理详解

配置pom文件

<?xml version="1.0" encoding="utf-8"?>
<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.0</modelversion>

 <groupid>bsea</groupid>
 <artifactid>springbootexception</artifactid>
 <version>1.0-snapshot</version>
 <packaging>jar</packaging>
 <parent>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-parent</artifactid>
  <version>1.5.14.release</version>
  <relativepath/> <!-- lookup parent from repository -->
 </parent>

 <properties>
  <project.build.sourceencoding>utf-8</project.build.sourceencoding>
  <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-web</artifactid>
  </dependency>

  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-test</artifactid>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-maven-plugin</artifactid>
   </plugin>
  </plugins>
 </build>
</project>

启动类

package com.zz;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;

@springbootapplication
public class app {
 public static void main(string[] args) {
  springapplication.run(app.class,args);
 }
}

自定义异常类

package com.zz.exception;

public class usernotexistexception extends runtimeexception{
 private static final long serialversionuid = -1574716826948451793l;

 private string id;

 public usernotexistexception(string id){
  super("user not exist");
  this.id = id;
 }

 public string getid() {
  return id;
 }

 public void setid(string id) {
  this.id = id;
 }
}

统一处理类

1. 在class上面加 @controlleradvice 表示全局异常处理类

2. 在方法的上面加@exceptionhandler(usernotexistexception.class), 表示catch 到这个异常类型,并且在方法中处理, 不同的异常,可以通过不同的方法处理,每个方法上面都是通过这个注解括号里面的异常类来设置需要处理的异常类型。

package com.zz.handler;

import com.zz.exception.usernotexistexception;
import org.springframework.http.httpstatus;
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.responsestatus;

import java.util.hashmap;
import java.util.map;

@controlleradvice
public class controllerexceptionhandler {
 //返回json的错误信息
 @exceptionhandler(usernotexistexception.class)
 @responsebody
 @responsestatus(httpstatus.internal_server_error)
 public map<string, object> handleusernotexistsexception(usernotexistexception e) {
  map<string, object> map = new hashmap<>();
  map.put("id", e.getid());
  map.put("message", e.getmessage());
  return map;
 }
 //错误以后,跳转到自己定义的错误页面
 @exceptionhandler(arithmeticexception.class)
 public string handle1(arithmeticexception e) {
  system.out.println("handle1 500*到了**************");
  return "/500.html";
 }
}

controller类

package com.zz.controller;

import com.zz.exception.usernotexistexception;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;

@restcontroller
@requestmapping("user")
public class usercontroller {

 @getmapping("/{id:\\d+}")
 public void get(@pathvariable string id) {

  throw new usernotexistexception(id);
 }
 @getmapping("error2")
 public void get2() {
  int a=1/0;
 }
}

#运行结果

SpringBoot 统一异常处理详解

SpringBoot 统一异常处理详解

代码地址 github: 添加链接描述

以上所述是小编给大家介绍的springboot统一异常处理详解整合,希望对大家有所帮助