spring-boot异常处理
Emm昨天台风停电就没写博客
Spring-boot异常处理
如果你在src/main/resource/templates目录下新建了error.html。那么当服务器出现异常的时候都会跳转到error.html。但是这样的方法,不适合我们开发
讲下另外一种方法
1.配置依pom.xml
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>18-spring-boot-exception5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.7</java.version>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
2.在启动器的子目录下新建Exception类 ,类实现HandlerExceptionResolver接口。重写resolveException方法
package cn.zds.ecception;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
/**
* 通过实现HandlerExceptionResolver接口做全局异常处理
*
*
*/
@Configuration//标记异常处理类
public class GlobalException implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) {
ModelAndView mv = new ModelAndView();
//判断不同异常类型,做不同视图跳转
if(ex instanceof ArithmeticException){//如果是算术异常
mv.setViewName("error1");//设置ModelAndView的跳转路径视图(error1.html)
}
if(ex instanceof NullPointerException){//空指针异常
mv.setViewName("error2");设置ModelAndView的跳转路径视图(error2.html)
}
mv.addObject("error", ex.toString());//将异常对象添加到ModelAndView
return mv;
}
}
3.在视图中可输出异常信息
error1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
出错了 1
<span th:text="${error}"></span>
</body>
</html>
error2.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
出错了 2
<span th:text="${error}"></span>
</body>
</html>
4.启动器App.java:
package cn.zds;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
*SpringBoot处理异常方式一:自定义错误页面
*
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
5.测试:
编写controller
package cn.zds.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ExceptionController {
//除0错误
@RequestMapping("error1")
public String error1(){
int a=10/0;
return "add";
}
//空指针错误
@RequestMapping("error2")
public String error2(){
String string=null;
string.contains("aa");
return "add";
}
}
6.项目结构:
还没有评论,来说两句吧...