SpringBoot异常处理和单元测试

学习目标

  • Spring Boot 异常处理
  • Spring Boot 单元测试

1.SpringBoot异常处理

1.1.自定义错误页面

SpringBoot默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制。一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求。在 springBoot 中提供了一个叫 BasicErrorController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常信息

在这里插入图片描述

如 果我 们 需 要 将 所 有 的 异 常 同 一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 再src/main/resources/

templates 目录下创建 error.html 页面。注意:名称必须叫 error

1.1.1.controller

/**
 * SpringBoot处理异常方式一:自定义错误页面
 */
@Controller
public class DemoController {
   
	
	@RequestMapping("/show")
	public String showInfo(){
   
		String str = null;
		str.length();
		return "index";
	}
	
	@RequestMapping("/show2")
	public String showInfo2(){
   
		int a = 10/0;
		return "index";
	}
}

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.创建全局异常处理器

/**
 * 通过实现HandlerExceptionResolver接口做全局异常处理
 */
@Component
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;
	}
}

1.2.3.错误页面

error1.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示页面-ArithmeticException</title>
</head>
<body>
	出错了,请与管理员联系。。。
	<span th:text="${error}"></span>
</body>
</html>

error2.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示页面-NullPointerException</title>
</head>
<body>
	出错了,请与管理员联系。。。
	<span th:text="${error}"></span>
</body>
</html>

1.3.整合ajax全局异常处理

1.3.1.创建全局异常处理器

@ControllerAdvice
public class AjaxGlobalException {
   
 
 
    /**
     * 处理全局异常
     * @param exception   异常
     * @return Map<String, Object>
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map<String, Object> errorHandler(Exception exception) {
   
        Map<String, Object> map = new HashMapMap<>();
        map.put("status", 500);
        map.put("msg", exception.getMessage());
        return map;
    }
}

2.Spring Boot整合Junit

2.1.Junit启动器

		<!--junit启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>

2.2.编写业务代码

2.2.1.dao

@Repository
public class UserDaoImpl {
   

	public void saveUser(){
   
		System.out.println("insert into users.....");
	}
}

2.2.2.service

@Service
public class UserServiceImpl {
   

	@Autowired
	private UserDaoImpl userDaoImpl;
	
	public void addUser(){
   
		this.userDaoImpl.saveUser();
	}
}

2.2.3.app

@SpringBootApplication
public class App {
   

	public static void main(String[] args) {
   
		SpringApplication.run(App.class, args);
	}
}

2.3.整合Junit

/**
 *  main方法:
 *		ApplicationContext ac=new 
 *       			ClassPathXmlApplicationContext("classpath:applicationContext.xml");
 *  junit与spring整合:
 *      @RunWith(SpringJUnit4ClassRunner.class):让junit与spring环境进行整合
 *   	@Contextconfiguartion("classpath:applicationContext.xml")  
 */
@RunWith(SpringJUnit4ClassRunner.class) 
@SpringBootTest(classes={
   App.class})
public class UserServiceTest {
   

	@Autowired
	private UserServiceImpl userServiceImpl;
	
	@Test
	public void testAddUser(){
   
		this.userServiceImpl.addUser();
	}
}

相关推荐

  1. SpringBoot异常处理单元测试

    2024-01-23 15:36:01       57 阅读
  2. springboot全局异常处理自定义异常处理

    2024-01-23 15:36:01       66 阅读
  3. 精通SpringBoot单元测试

    2024-01-23 15:36:01       46 阅读
  4. springboot 单元测试

    2024-01-23 15:36:01       44 阅读
  5. SpringBoot单元测试剖析

    2024-01-23 15:36:01       43 阅读
  6. SpringBoot单元测试

    2024-01-23 15:36:01       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-23 15:36:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-23 15:36:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-23 15:36:01       82 阅读
  4. Python语言-面向对象

    2024-01-23 15:36:01       91 阅读

热门阅读

  1. @RequestBody和@RequestParam和@PathVariable和@RequestAttribute

    2024-01-23 15:36:01       53 阅读
  2. 浪花 - 组队功能后端开发

    2024-01-23 15:36:01       52 阅读
  3. 【AI】深度学习在编码中的应用(11)

    2024-01-23 15:36:01       51 阅读
  4. 后端MySQL常用命令

    2024-01-23 15:36:01       46 阅读
  5. Docker consul

    2024-01-23 15:36:01       44 阅读
  6. 如何使用C++max函数(c语言max函数的使用方法)

    2024-01-23 15:36:01       61 阅读
  7. Vue前端规范【二】

    2024-01-23 15:36:01       38 阅读
  8. 【AI】深度学习在编码中的应用(10)

    2024-01-23 15:36:01       45 阅读
  9. 美易makeasy平台:小米汽车雄心勃勃

    2024-01-23 15:36:01       55 阅读
  10. C#控制台应用把视频转为图片

    2024-01-23 15:36:01       54 阅读
  11. 笨蛋学设计模式行为型模式-责任链模式【18】

    2024-01-23 15:36:01       55 阅读
  12. 笨蛋学设计模式行为型模式-解释器模式【23】

    2024-01-23 15:36:01       51 阅读
  13. 5.docker实战

    2024-01-23 15:36:01       54 阅读
  14. k8s-helm

    k8s-helm

    2024-01-23 15:36:01      48 阅读