本文主要是介绍Spring Boot全局异常捕捉!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目中避免不了有异常!
为了用户体验,常常把异常捕获起来,展现一个友好的页面,提醒用户!
在pom.xml中:
<?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>com.accord</groupId><artifactId>SpringBootHelloWorld</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>SpringBootHelloWorld</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.10.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-tomcat</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency><!-- 添加MySQL数据库驱动依赖包. --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- 添加Spring-data-jpa依赖. --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency></dependencies><build><plugins><!-- 添加springloader plugin --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin </artifactId><dependencies> <!--springloaded hot deploy --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.4.RELEASE</version></dependency> </dependencies> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
新建一个类GlobalDefaultExceptionHandler,在class注解上@ControllerAdvice,
在方法上注解上@ExceptionHandler(value = Exception.class),具体代码
import javax.servlet.http.HttpServletRequest;import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;/*** 1、新建一个Class,这里取名为GlobalDefaultExceptionHandler* 2、在class上添加注解,@ControllerAdvice;* 3、在class中添加一个方法* 4、在方法上添加@ExcetionHandler拦截相应的异常信息;* 5、如果返回的是View -- 方法的返回值是ModelAndView;* 6、如果返回的是String或者是Json数据,那么需要在方法上添加@ResponseBody注解.*/
@ControllerAdvice
public class GlobalDefaultExceptionHandler {@ExceptionHandler(Exception.class)@ResponseBodypublic String defaultExceptionHandler(HttpServletRequest req,Exception e){//是返回的String.//ModelAndView -- 介绍 模板引擎...? //ModelAndView mv = new ModelAndView();//mv.setViewName(viewName);return "对不起,服务器繁忙,请稍后再试!";}}
不捕获异常的页面如下:
捕获异常的页面如下:
对不起,服务器繁忙,请稍后再试!
这篇关于Spring Boot全局异常捕捉!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!