No exceptions can be avoided in the project.
For the user experience, the exception is often captured, showing a friendly page that alerts the user.
In the 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--> </pare Nt> <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.bo ot</groupid> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependenc Y> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-tomcat</ artifactid> <scope>provided</scope> </dependency> <dependency> <GROUPID>ORG.SPR Ingframework.boot</groupid> <artifactId>spring-boot-starter-test</artifactId> <scope> test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <ARTIFAC Tid>fastjson</artifactid> <version>1.2.15</version> </dependency> <!--add MySQL database drive Dynamic dependency Pack. --> <dependency> <GROUPID>mysql</groupid> <artifactId>mysql-connector-java</artifactId> </dependency> <!--Tim Add SPRING-DATA-JPA dependency. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-b
oot-starter-data-jpa</artifactid> </dependency> </dependencies> <build> <plugins> <!--add Springloader plugin--> <plugin> <groupid>org.springframework.boot</gro
upid> <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> <go
Al>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </executio n> </executions> </plugin> <plugin> <groupid>org.springframework.boo
t</groupid> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>
</build> </project>
Create a new class Globaldefaultexceptionhandler,
@controlleradvice on the class annotation,
On the annotation on the method @exceptionhandler (value = Exception.class), the specific code
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, a new class, here named Globaldefaultexceptionhandler
* 2, add annotations on class, @ControllerAdvice;
* 3, add a method in class
* 4, add @excetionhandler on the method to intercept the corresponding exception information;
* 5, if the return is view--the return value of the method is Modelandview;
* 6, if you return a string or JSON data, you need to add a @responsebody annotation to the method.
* *
@ControllerAdvice public
class Globaldefaultexceptionhandler {
@ExceptionHandler ( Exception.class)
@ResponseBody public
String Defaultexceptionhandler (httpservletrequest req,exception e) {
//is the returned string.
Modelandview--Introduction to Template engine ...?
Modelandview mv = new Modelandview ();
Mv.setviewname (viewName);
Return "Sorry, the server is busy, please try again later." ";
}
}
The pages that do not catch exceptions are as follows:
The page that catches the exception is as follows:
Sorry, the server is busy, please try again later.