SpringBoot 统一异常处理详解

发布时间:2020-09-20 23:18:54 作者:bseayin
来源:脚本之家 阅读:136

代码结构

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统一异常处理详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

推荐阅读:
  1. SpringBoot 统一异常处理
  2. 怎么在SpringBoot中实现统一异常处理

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot 统一 异常处理

上一篇:C#读写txt文件的2种方法

下一篇:Android使用GridView实现日历的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》