springboot实战笔记(十四)----springboot异常处理
程序员文章站
2022-03-08 17:35:52
...
一 前言
在系统中异常的出现是无可避免的,那么对于异常的处理,如何将出现异常后返回给前台不同的提示呢?而且以不同页面效果展示给前端呢?接下来说一下springboot其中常见两种处理异常的方式,自定义 HandlerExceptionResolver 的类处理异常。还有一个是@aaa@qq.com 注解处理异常。
二 自定义 HandlerExceptionResolver
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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
2.创建Controller
package com.bjsxt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UsersController {
@RequestMapping("/showUsers")
public String showUser(){
String str = null;
str.length();
//空指针异常
return "index";
}
@RequestMapping("/showUsers2")
public String showUser2(){
int a = 100/0;
//算术异常
return "index";
}
}
3.创建前端页面
error1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示页面-ArithmeticException</title>
</head>
<body>
<h3>算术异常提示页面</h3>
出错了,请与管理员联系。。。
<span th:text="${error}"></span>
</body>
</html>
error2.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示页面-NullPointerException</title>
</head>
<body>
<h3>空指针异常提示页面</h3>
出错了,请与管理员联系。。。
<span th:text="${error}"></span>
</body>
</html>
4.自定义 HandlerExceptionResolver 类
package com.bjsxt.exception;
import java.util.Properties;
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");
}
if(ex instanceof NullPointerException){
mv.setViewName("error2");
}
mv.addObject("error", ex.toString());
return mv;
}
}
5.启动测试
三 @aaa@qq.com 注解处理异常
package com.chp.springbootdemo.Exception;
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 MyControllerAdvice {
@ResponseBody
@ExceptionHandler(value = java.lang.Exception.class)
public Map<String, Object> myException(Exception ex) {
Map<String, Object> map = new HashMap<>();
map.put("code", 500);
map.put("msg", "出错了,请联系管理员");
return map;
}
@ResponseBody
@ExceptionHandler(value = java.lang.NullPointerException.class)
public Map<String, Object> myException2(Exception ex) {
Map<String, Object> map = new HashMap<>();
map.put("code", -500);
map.put("msg", "空指针异常");
return map;
}
//自定义异常
@ResponseBody
@ExceptionHandler(value=com.chp.springbootdemo.Exception.MyException.class)
public Map<String, Object> myException3(Exception ex) {
Map<String, Object> map = new HashMap<>();
map.put("code", -800);
map.put("msg", ex.getMessage());
return map;
}
}
自定义的异常:
package com.chp.springbootdemo.Exception;
public class MyException extends RuntimeException {
public MyException() {
super();
// TODO Auto-generated constructor stub
}
public MyException(String message, Throwable cause) {
super(message, cause);
}
}