周振林 周振林
首页
  • 前端文章

    • HTML
    • CSS
    • Tailwind CSS (opens new window)
    • JavaScript
    • Vue3
    • 其他
  • 学习笔记

    • 《JavaScript教程》
    • 《ES6 教程》
    • 《TypeScript》
    • 《Vue》
    • 《Git》
    • 《小程序笔记》
    • 《JS设计模式总结笔记》
  • 规范
  • Spring
  • 安装教程
  • 其他教程
  • 归真医学
  • 常用药材
  • 学习笔记
  • 经方学习心得
  • 基础
  • 虚拟化
  • Docker
  • OpenStack
  • 心情杂货
关于
收藏
  • 分类
  • 标签
  • 归档

周振林

IT界的小学生
首页
  • 前端文章

    • HTML
    • CSS
    • Tailwind CSS (opens new window)
    • JavaScript
    • Vue3
    • 其他
  • 学习笔记

    • 《JavaScript教程》
    • 《ES6 教程》
    • 《TypeScript》
    • 《Vue》
    • 《Git》
    • 《小程序笔记》
    • 《JS设计模式总结笔记》
  • 规范
  • Spring
  • 安装教程
  • 其他教程
  • 归真医学
  • 常用药材
  • 学习笔记
  • 经方学习心得
  • 基础
  • 虚拟化
  • Docker
  • OpenStack
  • 心情杂货
关于
收藏
  • 分类
  • 标签
  • 归档
  • 规范

  • Spring

    • Spring基础
    • Spring IoC
    • Spring AOP
    • SpringBoot异常
      • Spring 异常
        • 案例
    • SpringBoot过滤器
    • SpringBoot拦截器
    • Response设置响应编码
    • 依赖start和依赖BOM区别
    • Thymeleaf教程
  • 安装教程

  • 其他教程

  • 后端
  • Spring
周振林
2024-05-22
目录

SpringBoot异常

# Spring 异常

在Spring中使用 @ControllerAdvice和@ExceptionHandler处理全局异常。

# 案例

全局异常处理

GlobalExceptionHandler

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(IllegalArgumentException.class)
    public Object illegalHandler(IllegalArgumentException e){
        System.out.println("进入illegalHandler");
        
        return ResponseEntity.status(400).body(R.failed(e.getMessage()));
    }

    @ExceptionHandler(Exception.class)
    public Object common(Exception e){
        System.out.println("进入commonHandler");
        return ResponseEntity.status(400).body(e.getMessage());
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

测试异常 HelloController

@RestController
@RequestMapping("/api")
public class HelloController {

    @RequestMapping("/data")
    public Object data(){
        return R.ok("恭喜你,成功获取到数据");
    }
    @RequestMapping("/hello")
    public Object hello(){
        throw  new IllegalArgumentException("无效参数");
    }

    @RequestMapping("/common")
    public Object common(){
        int i=5/0;

        return "common";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

R


@Data
@NoArgsConstructor
@AllArgsConstructor
public class R {

    private String code;
    private String message;
    private Object data;

    public R(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public static R ok(){
        return ok(null);
    }
    public static R ok(Object data){
        R r=new R("0","操作成功",data);
        return r;
    }
    public static R failed(){

        return failed(null);
    }
    public static R failed(Object data){
        R r=new R("1","操作失败",data);
        return r;
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

pom 文件

<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<optional>true</optional>
</dependency>
1
2
3
4
5
6
7
8
9
10

源码地址 (opens new window)

Last Updated: 2024/07/26, 16:12:04
Spring AOP
SpringBoot过滤器

← Spring AOP SpringBoot过滤器→

最近更新
01
Docker安装
06-10
02
Docker运行JAR
06-10
03
Docker部署MySQL
06-10
更多文章>
Copyright © 2019-2025 鲁ICP备19032096号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式