SpringBoot 統(tǒng)一異常處理詳解
代碼結(jié)構(gòu)
配置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>
啟動(dòng)類(lèi)
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); } }
自定義異常類(lèi)
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; } }
統(tǒng)一處理類(lèi)
1. 在class上面加 @ControllerAdvice 表示全局異常處理類(lèi)
2. 在方法的上面加@ExceptionHandler(UserNotExistException.class), 表示catch 到這個(gè)異常類(lèi)型,并且在方法中處理, 不同的異常,可以通過(guò)不同的方法處理,每個(gè)方法上面都是通過(guò)這個(gè)注解括號(hào)里面的異常類(lèi)來(lái)設(shè)置需要處理的異常類(lèi)型。
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的錯(cuò)誤信息 @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; } //錯(cuò)誤以后,跳轉(zhuǎn)到自己定義的錯(cuò)誤頁(yè)面 @ExceptionHandler(ArithmeticException.class) public String handle1(ArithmeticException e) { System.out.println("handle1 500*到了**************"); return "/500.html"; } }
controller類(lèi)
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; } }
#運(yùn)行結(jié)果
代碼地址 github: 添加鏈接描述
以上所述是小編給大家介紹的SpringBoot統(tǒng)一異常處理詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring中的之啟動(dòng)過(guò)程obtainFreshBeanFactory詳解
這篇文章主要介紹了Spring中的之啟動(dòng)過(guò)程obtainFreshBeanFactory詳解,在refresh時(shí),prepareRefresh后,馬上就調(diào)用了obtainFreshBeanFactory創(chuàng)建beanFactory以及掃描bean信息(beanDefinition),并通過(guò)BeanDefinitionRegistry注冊(cè)到容器中,需要的朋友可以參考下2024-02-02Java 如何使用Feign發(fā)送HTTP請(qǐng)求
這篇文章主要介紹了Java 如何使用Feign發(fā)送HTTP請(qǐng)求,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下2020-11-11java代碼關(guān)閉tomcat程序及出現(xiàn)問(wèn)題解析
這篇文章主要介紹了java代碼關(guān)閉tomcat程序 及出現(xiàn)問(wèn)題解析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-05-05使用springboot訪問(wèn)圖片本地路徑并映射成url
這篇文章主要介紹了使用springboot訪問(wèn)圖片本地路徑并映射成url的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08java返回json請(qǐng)求中文變成問(wèn)號(hào)的問(wèn)題及解決
這篇文章主要介紹了java返回json請(qǐng)求中文變成問(wèn)號(hào)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07解決@Scope(“prototype“)不生效的問(wèn)題
這篇文章主要介紹了解決@Scope(“prototype“)不生效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06