一、Spring Boot异常处理
1.1、自定义错误页面
SpringBoot默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制。一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求。在 springBoot 中提供了一个叫 BasicErrorController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常信息
如 果我 们 需 要 将 所 有 的 异 常 同 一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 再src/main/resources/templates 目录下创建 error.html 页面。注意:名称必须叫 error
1.1.1、controller
package com.by.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ExceptionController {
@RequestMapping("show1")
public String exception1(){
String str=null;
str.length();
return "exception1";
}
@RequestMapping("show2")
public String exception2(){
int a=6/0;
return "exception2";
}
}
1.1.2、错误页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示页面</title>
</head>
<body>
出错了,请与管理员联系。。。
<span th:text="${error}"></span>
</body>
</html>
1.2、整合web全局异常处理器
1.2.1、处理思路
1.2.2、创建全局异常处理器
package com.by.exception;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
//发短信、发邮件
//指定跳转的错误页面
ModelAndView mv = new ModelAndView();
if (e instanceof NullPointerException){
mv.setViewName("exception1");
}else if (e instanceof ArithmeticException){
mv.setViewName("exception2");
}
mv.addObject("msg",e.toString());
return mv;
}
}
1.2.3、错误页面
exception1
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示页面-NullPointerException</title>
</head>
<body>
出错了,请与管理员联系。。。
<span th:text="${msg}"></span>
</body>
</html>
exception2
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示页面-ArithmeticException</title>
</head>
<body>
出错了,请与管理员联系。。。
<span th:text="${msg}"></span>
</body>
</html>
1.3、整合ajax全局异常处理
1.3.1、创建全局异常处理器
package com.by.exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
//Controller增强 报错到异常方法
@ControllerAdvice
public class AjaxGlobalExceptionResolver {
@ResponseBody
//异常方法
@ExceptionHandler
public Map exceptionHandler(Exception e){
HashMap<String, Object> map = new HashMap<>();
map.put("status",500);
map.put("msg",e.toString());
return map;
}
}
controller
package com.by.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class AjaxExceptionController {
@RequestMapping("/exception3")
public Map exception3(){
HashMap<String, Object> map = new HashMap<>();
map.put("status",200);
map.put("msg","世界你好");
int a=6/0;
return map;
}
}
二、Spring Boot整合Junit
2.1、Junit启动器
<!--junit启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2.2、编写业务代码
dao
package com.by.dao;
public interface UserDao {
void addUser();
}
package com.by.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao{
@Override
public void addUser() {
System.out.println("insert into user...");
}
}
service
package com.by.service;
public interface UserService {
void addUser();
}
package com.by.service;
import com.by.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void addUser() {
userDao.addUser();
}
}
web
package com.by.web;
import com.by.SpringJunitApplication;
import com.by.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
//junit启动springboot
@SpringBootTest(classes = {SpringJunitApplication.class})
public class Servlet {
@Autowired
private UserService userService;
@Test
public void addUser(){
userService.addUser();
}
}
application
package com.by;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringJunitApplication {
public static void main(String[] args) {
SpringApplication.run(SpringJunitApplication.class);
}
}