一、 SpringBoot 中異常處理方式
1 SpringBoot 中對於異常處理提供了五種處理方式
1.1自定義錯誤頁面-----不適用與生產開發環境
1.2@ExceptionHandle 註解處理異常-----異常多時易造成代碼冗餘
@ExceptionHandler(value={java.lang.ArithmeticException.class})
1.2.1 Controller
package com.bjsxt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* 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";
}
/**
* java.lang.ArithmeticException
* 該方法需要返回一個ModelAndView:目的是可以讓我們封裝異常信息以及視圖的指定
* 參數Exception e:會將產生異常對象注入到方法中
*/
@ExceptionHandler(value={java.lang.ArithmeticException.class})
public ModelAndView arithmeticExceptionHandler(Exception e){
ModelAndView mv = new ModelAndView();
mv.addObject("error", e.toString());
mv.setViewName("error1");
return mv;
}
/**
* java.lang.NullPointerException
* 該方法需要返回一個ModelAndView:目的是可以讓我們封裝異常信息以及視圖的指定
* 參數Exception e:會將產生異常對象注入到方法中
*/
@ExceptionHandler(value={java.lang.NullPointerException.class})
public ModelAndView nullPointerExceptionHandler(Exception e){
ModelAndView mv = new ModelAndView();
mv.addObject("error", e.toString());
mv.setViewName("error2");
return mv;
}
}
1.2.2 頁面
1.3@ControllerAdvice+@ExceptionHandler 註解處理----可以傳遞異常參數
1.3.1 需要創建一個能夠處理異常的全局異常類。在該類上需要添加@ControllerAdvice 註解
package com.bjsxt.exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
/**
* 全局異常處理類
*
*
*/
@ControllerAdvice
public class GlobalException {
/**
* java.lang.ArithmeticException
* 該方法需要返回一個ModelAndView:目的是可以讓我們封裝異常信息以及視圖的指定
* 參數Exception e:會將產生異常對象注入到方法中
*/
@ExceptionHandler(value={java.lang.ArithmeticException.class})
public ModelAndView arithmeticExceptionHandler(Exception e){
ModelAndView mv = new ModelAndView();
mv.addObject("error", e.toString());
mv.setViewName("error1");
return mv;
}
/**
* java.lang.NullPointerException
* 該方法需要返回一個ModelAndView:目的是可以讓我們封裝異常信息以及視圖的指定
* 參數Exception e:會將產生異常對象注入到方法中
*/
@ExceptionHandler(value={java.lang.NullPointerException.class})
public ModelAndView nullPointerExceptionHandler(Exception e){
ModelAndView mv = new ModelAndView();
mv.addObject("error", e.toString());
mv.setViewName("error2");
return mv;
}
}
1.4配置 SimpleMappingExceptionResolver 處理異常-----不能傳遞異常參數
1.4.1 在全局異常類中添加一個方法完成異常的統一處理
package com.bjsxt.exception;
import java.util.Properties;
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.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
/**
* 通過SimpleMappingExceptionResolver做全局異常處理
*
*
*/
@Configuration
public class GlobalException {
/**
* 該方法必須要有返回值。返回值類型必須是:SimpleMappingExceptionResolver
*/
@Bean
public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){
SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
Properties mappings = new Properties();
/**
* 參數一:異常的類型,注意必須是異常類型的全名
* 參數二:視圖名稱
*/
mappings.put("java.lang.ArithmeticException", "error1");
mappings.put("java.lang.NullPointerException","error2");
//設置異常與視圖映射信息的
resolver.setExceptionMappings(mappings);
return resolver;
}
}
1.5自定義 HandlerExceptionResolver 類處理異常----簡單又可以傳遞異常參數和視圖
1.5.1 需要在全局異常處理類中實現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;
}
}
二、 Spring Boot 整合 Junit 單元測試
1 創建項目
2 修改 pom 文件
<!-- 添加junit環境的jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
3 編寫業務代碼
3.1Dao
package com.bjsxt.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl {
public void saveUser(){
System.out.println("insert into users.....");
}
}
3.2業務層
package com.bjsxt.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bjsxt.dao.UserDaoImpl;
@Service
public class UserServiceImpl {
@Autowired
private UserDaoImpl userDaoImpl;
public void addUser(){
this.userDaoImpl.saveUser();
}
}
3.3編寫啓動類
package com.bjsxt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
4 使用 SpringBoot 整合 Junit 做單元
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={App.class})
4.1編寫測試類
package com.bjsxt.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.bjsxt.App;
import com.bjsxt.service.UserServiceImpl;
/**
* SpringBoot測試類
*@RunWith:啓動器
*SpringJUnit4ClassRunner.class:讓junit與spring環境進行整合
*
*@SpringBootTest(classes={App.class}) 1,當前類爲springBoot的測試類
*@SpringBootTest(classes={App.class}) 2,加載SpringBoot啓動類。啓動springBoot
*
*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();
}
}