SpringBoot(十四)-----异常处理

SpringBoot一共有五种异常处理的方式

方式一:@ControllerAdvice+@ExceptionHandler 注解处理异常

 

GlobalExceptionHandler.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.zk.myspringboot002;
 
import java.util.HashMap;
import java.util.Map;
 
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
 
//全局捕获异常
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Map<String,Object> resultError(){
        Map<String,Object> result=new HashMap<String,Object>();
        result.put("errorCode", "500");
        result.put("errorMsg", "系统错误");
        return result;
    }
}

 SpringBootApplicationTwice.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.zk.myspringboot002;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 
@EnableAutoConfiguration
@SpringBootApplication
public class SpringBootApplicationTwice {
    public static void main(String[]args){
        SpringApplication.run(SpringBootApplicationTwice.class, args);
    }
    public static void run(String...arg0) {
        System.out.println("Hello world from Command Line Runner");
    }
}

 TestController.java

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
33
34
35
package com.zk.myspringboot002;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
//标识该接口全部返回json格式
@RestController
public class TestController {
     
    @RequestMapping("/success")
    public String success() {
        return "success";
    }
     
    @RequestMapping("/getMap")
    public Map<String,Object> index() {
        Map<String,Object> result=new HashMap<String,Object>();
        result.put("errorCode", "200");
        result.put("errorMsg", "message");
        return result;
    }
     
     
     
    @RequestMapping("/fail")
    public String error() {
        int i=1/0;
        return "error";
    }
}

  pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>myspringboot002</groupId>
  <artifactId>myspringboot002</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
  </dependencies>
</project>

  运行结果如下:

 

 

 

 

 

posted @   leagueandlegends  阅读(99)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示