1,統(tǒng)一異常處理
1. 問題描述
在講解這一部分知識點之前,我們先來演示個效果,修改BookController類的getById
方法
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id) {
//手動添加一個錯誤信息
if(id==1){
int i = 1/0;
}
Book book = bookService.getById(id);
Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
String msg = book != null ? "" : "數(shù)據(jù)查詢失敗,請重試!";
return new Result(code,book,msg);
}
重新啟動運行項目,使用PostMan發(fā)送請求,當傳入的id為1,則會出現(xiàn)如下效果:
前端接收到這個信息后和之前我們約定的格式不一致,這個問題該如何解決?
在解決問題之前,我們先來看下異常的種類及出現(xiàn)異常的原因:
- 框架內(nèi)部拋出的異常:因使用不合規(guī)導(dǎo)致
- 數(shù)據(jù)層拋出的異常:因外部服務(wù)器故障導(dǎo)致(例如:服務(wù)器訪問超時)
- 業(yè)務(wù)層拋出的異常:因業(yè)務(wù)邏輯書寫錯誤導(dǎo)致(例如:遍歷業(yè)務(wù)書寫操作,導(dǎo)致索引異常等)
- 表現(xiàn)層拋出的異常:因數(shù)據(jù)收集、校驗等規(guī)則導(dǎo)致(例如:不匹配的數(shù)據(jù)類型間導(dǎo)致異常)
- 工具類拋出的異常:因工具類書寫不嚴謹不夠健壯導(dǎo)致(例如:必要釋放的連接長期未釋放等)
看完上面這些出現(xiàn)異常的位置,你會發(fā)現(xiàn),在我們開發(fā)的任何一個位置都有可能出現(xiàn)異常,而且這些異常是不能避免的。所以我們就得將異常進行處理。
思考
-
各個層級均出現(xiàn)異常,異常處理代碼書寫在哪一層?
所有的異常均拋出到表現(xiàn)層進行處理
-
異常的種類很多,表現(xiàn)層如何將所有的異常都處理到呢?
異常分類
-
表現(xiàn)層處理異常,每個方法中單獨書寫,代碼書寫量巨大且意義不強,如何解決?
AOP
對于上面這些問題及解決方案,SpringMVC已經(jīng)為我們提供了一套解決方案:
-
異常處理器:
- 集中的、統(tǒng)一的處理項目中出現(xiàn)的異常。
2. 異常處理器的使用
1. 環(huán)境準備
- 創(chuàng)建一個Web的Maven項目
- pom.xml添加SSM整合所需jar包
- 創(chuàng)建對應(yīng)的配置類
- 編寫Controller、Service接口、Service實現(xiàn)類、Dao接口和模型類
- resources下提供jdbc.properties配置文件
內(nèi)容參考前面的項目或者直接使用前面的項目進行本節(jié)內(nèi)容的學(xué)習(xí)。
最終創(chuàng)建好的項目結(jié)構(gòu)如下:
2. 使用步驟
步驟1:創(chuàng)建異常處理器類
//@RestControllerAdvice用于標識當前類為REST風(fēng)格對應(yīng)的異常處理器
@RestControllerAdvice
public class ProjectExceptionAdvice {
//除了自定義的異常處理器,保留對Exception類型的異常處理,用于處理非預(yù)期的異常
@ExceptionHandler(Exception.class)
public void doException(Exception ex){
System.out.println("嘿嘿,異常你哪里跑!")
}
}
確保SpringMvcConfig能夠掃描到異常處理器類
步驟2:讓程序拋出異常
修改BookController
的getById方法,添加int i = 1/0
.
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id) {
int i = 1/0;
Book book = bookService.getById(id);
Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
String msg = book != null ? "" : "數(shù)據(jù)查詢失敗,請重試!";
return new Result(code,book,msg);
}
步驟3:運行程序,測試
說明異常已經(jīng)被攔截并執(zhí)行了doException
方法。
異常處理器類返回結(jié)果給前端
//@RestControllerAdvice用于標識當前類為REST風(fēng)格對應(yīng)的異常處理器
@RestControllerAdvice
public class ProjectExceptionAdvice {
//除了自定義的異常處理器,保留對Exception類型的異常處理,用于處理非預(yù)期的異常
@ExceptionHandler(Exception.class)
public Result doException(Exception ex){
System.out.println("嘿嘿,異常你哪里跑!")
return new Result(666,null,"嘿嘿,異常你哪里跑!");
}
}
啟動運行程序,測試
至此,就算后臺執(zhí)行的過程中拋出異常,最終也能按照我們和前端約定好的格式返回給前端。
知識點1:@RestControllerAdvice
名稱 | @RestControllerAdvice |
---|---|
類型 | 類注解 |
位置 | Rest風(fēng)格開發(fā)的控制器增強類定義上方 |
作用 | 為Rest風(fēng)格開發(fā)的控制器類做增強 |
說明: 此注解自帶@ResponseBody注解與@Component注解,具備對應(yīng)的功能
知識點2:@ExceptionHandler
名稱 | @ExceptionHandler |
---|---|
類型 | 方法注解 |
位置 | 專用于異常處理的控制器方法上方 |
作用 | 設(shè)置指定異常的處理方案,功能等同于控制器方法, 出現(xiàn)異常后終止原始控制器執(zhí)行,并轉(zhuǎn)入當前方法執(zhí)行 |
說明 此類方法可以根據(jù)處理的異常不同,制作多個方法分別處理對應(yīng)的異常
3. 項目異常處理方案
1. 異常分類
異常處理器我們已經(jīng)能夠使用了,那么在咱們的項目中該如何來處理異常呢?
因為異常的種類有很多,如果每一個異常都對應(yīng)一個@ExceptionHandler,那得寫多少個方法來處理各自的異常,所以我們在處理異常之前,需要對異常進行一個分類:
-
業(yè)務(wù)異常(BusinessException)
-
規(guī)范的用戶行為產(chǎn)生的異常
- 用戶在頁面輸入內(nèi)容的時候未按照指定格式進行數(shù)據(jù)填寫,如在年齡框輸入的是字符串
-
-
不規(guī)范的用戶行為操作產(chǎn)生的異常
- 如用戶故意傳遞錯誤數(shù)據(jù)
-
系統(tǒng)異常(SystemException)
- 項目運行過程中可預(yù)計但無法避免的異常
- 比如數(shù)據(jù)庫或服務(wù)器宕機
- 項目運行過程中可預(yù)計但無法避免的異常
-
其他異常(Exception)
- 編程人員未預(yù)期到的異常,如:用到的文件不存在
將異常分類以后,針對不同類型的異常,要提供具體的解決方案:
2. 異常解決方案
- 業(yè)務(wù)異常(BusinessException)
- 發(fā)送對應(yīng)消息傳遞給用戶,提醒規(guī)范操作
- 大家常見的就是提示用戶名已存在或密碼格式不正確等
- 發(fā)送對應(yīng)消息傳遞給用戶,提醒規(guī)范操作
- 系統(tǒng)異常(SystemException)
- 發(fā)送固定消息傳遞給用戶,安撫用戶
- 系統(tǒng)繁忙,請稍后再試
- 系統(tǒng)正在維護升級,請稍后再試
- 系統(tǒng)出問題,請聯(lián)系系統(tǒng)管理員等
- 發(fā)送特定消息給運維人員,提醒維護
- 可以發(fā)送短信、郵箱或者是公司內(nèi)部通信軟件
- 記錄日志
- 發(fā)消息和記錄日志對用戶來說是不可見的,屬于后臺程序
- 發(fā)送固定消息傳遞給用戶,安撫用戶
- 其他異常(Exception)
- 發(fā)送固定消息傳遞給用戶,安撫用戶
- 發(fā)送特定消息給編程人員,提醒維護(納入預(yù)期范圍內(nèi))
- 一般是程序沒有考慮全,比如未做非空校驗等
- 記錄日志
3. 異常解決方案的具體實現(xiàn)
思路:
1.先通過自定義異常,完成BusinessException和SystemException的定義
2.將其他異常包裝成自定義異常類型
3.在異常處理器類中對不同的異常進行處理
步驟1:自定義異常類
//自定義異常處理器,用于封裝異常信息,對異常進行分類
public class SystemException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public SystemException(Integer code, String message) {
super(message);
this.code = code;
}
public SystemException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
//自定義異常處理器,用于封裝異常信息,對異常進行分類
public class BusinessException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public BusinessException(Integer code, String message) {
super(message);
this.code = code;
}
public BusinessException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
說明:
- 讓自定義異常類繼承
RuntimeException
的好處是,后期在拋出這兩個異常的時候,就不用在try…catch…或throws了 - 自定義異常類中添加
code
屬性的原因是為了更好的區(qū)分異常是來自哪個業(yè)務(wù)的
步驟2:將其他異常包成自定義異常
假如在BookServiceImpl的getById方法拋異常了,該如何來包裝呢?
public Book getById(Integer id) {
//模擬業(yè)務(wù)異常,包裝成自定義異常
if(id == 1){
throw new BusinessException(Code.BUSINESS_ERR,"請不要使用你的技術(shù)挑戰(zhàn)我的耐性!");
}
//模擬系統(tǒng)異常,將可能出現(xiàn)的異常進行包裝,轉(zhuǎn)換成自定義異常
try{
int i = 1/0;
}catch (Exception e){
throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服務(wù)器訪問超時,請重試!",e);
}
return bookDao.getById(id);
}
具體的包裝方式有:
- 方式一:
try{}catch(){}
在catch中重新throw我們自定義異常即可。 - 方式二:直接throw自定義異常即可
上面為了使code
看著更專業(yè)些,我們在Code類中再新增需要的屬性
//狀態(tài)碼
public class Code {
public static final Integer SAVE_OK = 20011;
public static final Integer DELETE_OK = 20021;
public static final Integer UPDATE_OK = 20031;
public static final Integer GET_OK = 20041;
public static final Integer SAVE_ERR = 20010;
public static final Integer DELETE_ERR = 20020;
public static final Integer UPDATE_ERR = 20030;
public static final Integer GET_ERR = 20040;
public static final Integer SYSTEM_ERR = 50001;
public static final Integer SYSTEM_TIMEOUT_ERR = 50002;
public static final Integer SYSTEM_UNKNOW_ERR = 59999;
public static final Integer BUSINESS_ERR = 60002;
}
步驟3:處理器類中處理自定義異常
//@RestControllerAdvice用于標識當前類為REST風(fēng)格對應(yīng)的異常處理器
@RestControllerAdvice
public class ProjectExceptionAdvice {
//@ExceptionHandler用于設(shè)置當前處理器類對應(yīng)的異常類型
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException ex){
//記錄日志
//發(fā)送消息給運維
//發(fā)送郵件給開發(fā)人員,ex對象發(fā)送給開發(fā)人員
return new Result(ex.getCode(),null,ex.getMessage());
}
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(BusinessException ex){
return new Result(ex.getCode(),null,ex.getMessage());
}
//除了自定義的異常處理器,保留對Exception類型的異常處理,用于處理非預(yù)期的異常
@ExceptionHandler(Exception.class)
public Result doOtherException(Exception ex){
//記錄日志
//發(fā)送消息給運維
//發(fā)送郵件給開發(fā)人員,ex對象發(fā)送給開發(fā)人員
return new Result(Code.SYSTEM_UNKNOW_ERR,null,"系統(tǒng)繁忙,請稍后再試!");
}
}
步驟4:運行程序
根據(jù)ID查詢,
如果傳入的參數(shù)為1,會報BusinessException
如果傳入的是其他參數(shù),會報SystemException
對于異常我們就已經(jīng)處理完成了,不管后臺哪一層拋出異常,都會以我們與前端約定好的方式進行返回,前端只需要把信息獲取到,根據(jù)返回的正確與否來展示不同的內(nèi)容即可。
小結(jié)
以后項目中的異常處理方式為:
2,前后臺協(xié)議聯(lián)調(diào)
1. 環(huán)境準備
- 創(chuàng)建一個Web的Maven項目
- pom.xml添加SSM整合所需jar包
- 創(chuàng)建對應(yīng)的配置類
- 編寫Controller、Service接口、Service實現(xiàn)類、Dao接口和模型類
- resources下提供jdbc.properties配置文件
內(nèi)容參考前面的項目或者直接使用前面的項目進行本節(jié)內(nèi)容的學(xué)習(xí)。
最終創(chuàng)建好的項目結(jié)構(gòu)如下:
- 將
資料\SSM功能頁面
下面的靜態(tài)資源拷貝到webapp下。
鏈接:https://pan.baidu.com/s/1Nu1jT–H00csTU48c7TyVg?pwd=0kfh
提取碼:0kfh
- 因為添加了靜態(tài)資源,SpringMVC會攔截,所有需要在SpringConfig的配置類中將靜態(tài)資源進行放行。
-
新建SpringMvcSupport
@Configuration public class SpringMvcSupport extends WebMvcConfigurationSupport { @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/pages/**").addResourceLocations("/pages/"); registry.addResourceHandler("/css/**").addResourceLocations("/css/"); registry.addResourceHandler("/js/**").addResourceLocations("/js/"); registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/"); } }
-
在SpringMvcConfig中掃描SpringMvcSupport
@Configuration @ComponentScan({"com.itheima.controller","com.itheima.config"}) @EnableWebMvc public class SpringMvcConfig { }
接下來我們就需要將所有的列表查詢、新增、修改、刪除等功能一個個來實現(xiàn)下。
2. 列表功能
需求:頁面加載完后發(fā)送異步請求到后臺獲取列表數(shù)據(jù)進行展示。
1.找到頁面的鉤子函數(shù),
created()
2.
created()
方法中調(diào)用了this.getAll()
方法3.在getAll()方法中使用axios發(fā)送異步請求從后臺獲取數(shù)據(jù)
4.訪問的路徑為
http://localhost/books
5.返回數(shù)據(jù)
返回數(shù)據(jù)res.data的內(nèi)容如下:
{
"data": [
{
"id": 1,
"type": "計算機理論",
"name": "Spring實戰(zhàn) 第五版",
"description": "Spring入門經(jīng)典教程,深入理解Spring原理技術(shù)內(nèi)幕"
},
{
"id": 2,
"type": "計算機理論",
"name": "Spring 5核心原理與30個類手寫實踐",
"description": "十年沉淀之作,手寫Spring精華思想"
},...
],
"code": 20041,
"msg": ""
}
發(fā)送方式:
getAll() {
//發(fā)送ajax請求
axios.get("/books").then((res)=>{
this.dataList = res.data.data;
});
}
3. 添加功能
需求:完成圖片的新增功能模塊
1.找到頁面上的
新建
按鈕,按鈕上綁定了@click="handleCreate()"
方法2.在method中找到
handleCreate
方法,方法中打開新增面板3.新增面板中找到
確定
按鈕,按鈕上綁定了@click="handleAdd()"
方法4.在method中找到
handleAdd
方法5.在方法中發(fā)送請求和數(shù)據(jù),響應(yīng)成功后將新增面板關(guān)閉并重新查詢數(shù)據(jù)
handleCreate
打開新增面板
handleCreate() {
this.dialogFormVisible = true;
},
handleAdd
方法發(fā)送異步請求并攜帶數(shù)據(jù)
handleAdd () {
//發(fā)送ajax請求
//this.formData是表單中的數(shù)據(jù),最后是一個json數(shù)據(jù)
axios.post("/books",this.formData).then((res)=>{
this.dialogFormVisible = false;
this.getAll();
});
}
4. 添加功能狀態(tài)處理
基礎(chǔ)的新增功能已經(jīng)完成,但是還有一些問題需要解決下:
需求:新增成功是關(guān)閉面板,重新查詢數(shù)據(jù),那么新增失敗以后該如何處理?
1.在handlerAdd方法中根據(jù)后臺返回的數(shù)據(jù)來進行不同的處理
2.如果后臺返回的是成功,則提示成功信息,并關(guān)閉面板
3.如果后臺返回的是失敗,則提示錯誤信息
(1)修改前端頁面
handleAdd () {
//發(fā)送ajax請求
axios.post("/books",this.formData).then((res)=>{
//如果操作成功,關(guān)閉彈層,顯示數(shù)據(jù)
if(res.data.code == 20011){
this.dialogFormVisible = false;
this.$message.success("添加成功");
}else if(res.data.code == 20010){
this.$message.error("添加失敗");
}else{
this.$message.error(res.data.msg);
}
}).finally(()=>{
this.getAll();
});
}
(2)后臺返回操作結(jié)果,將Dao層的增刪改方法返回值從void
改成int
public interface BookDao {
// @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
@Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
public int save(Book book);
@Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
public int update(Book book);
@Delete("delete from tbl_book where id = #{id}")
public int delete(Integer id);
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
@Select("select * from tbl_book")
public List<Book> getAll();
}
(3)在BookServiceImpl中,增刪改方法根據(jù)DAO的返回值來決定返回true/false
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
public boolean save(Book book) {
return bookDao.save(book) > 0;
}
public boolean update(Book book) {
return bookDao.update(book) > 0;
}
public boolean delete(Integer id) {
return bookDao.delete(id) > 0;
}
public Book getById(Integer id) {
if(id == 1){
throw new BusinessException(Code.BUSINESS_ERR,"請不要使用你的技術(shù)挑戰(zhàn)我的耐性!");
}
// //將可能出現(xiàn)的異常進行包裝,轉(zhuǎn)換成自定義異常
// try{
// int i = 1/0;
// }catch (Exception e){
// throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服務(wù)器訪問超時,請重試!",e);
// }
return bookDao.getById(id);
}
public List<Book> getAll() {
return bookDao.getAll();
}
}
(4)測試錯誤情況,將圖書類別長度設(shè)置超出范圍即可
處理完新增后,會發(fā)現(xiàn)新增還存在一個問題,
新增成功后,再次點擊新增
按鈕會發(fā)現(xiàn)之前的數(shù)據(jù)還存在,這個時候就需要在新增的時候?qū)⒈韱蝺?nèi)容清空。
resetForm(){
this.formData = {};
}
handleCreate() {
this.dialogFormVisible = true;
this.resetForm();
}
5. 修改功能
需求:完成圖書信息的修改功能
1.找到頁面中的
編輯
按鈕,該按鈕綁定了@click="handleUpdate(scope.row)"
2.在method的
handleUpdate
方法中發(fā)送異步請求根據(jù)ID查詢圖書信息3.根據(jù)后臺返回的結(jié)果,判斷是否查詢成功
? 如果查詢成功打開修改面板回顯數(shù)據(jù),如果失敗提示錯誤信息
4.修改完成后找到修改面板的
確定
按鈕,該按鈕綁定了@click="handleEdit()"
5.在method的
handleEdit
方法中發(fā)送異步請求提交修改數(shù)據(jù)6.根據(jù)后臺返回的結(jié)果,判斷是否修改成功
? 如果成功提示錯誤信息,關(guān)閉修改面板,重新查詢數(shù)據(jù),如果失敗提示錯誤信息
scope.row代表的是當前行的行數(shù)據(jù),也就是說,scope.row就是選中行對應(yīng)的json數(shù)據(jù),如下:
{
"id": 1,
"type": "計算機理論",
"name": "Spring實戰(zhàn) 第五版",
"description": "Spring入門經(jīng)典教程,深入理解Spring原理技術(shù)內(nèi)幕"
}
修改handleUpdate
方法
//彈出編輯窗口
handleUpdate(row) {
// console.log(row); //row.id 查詢條件
//查詢數(shù)據(jù),根據(jù)id查詢
axios.get("/books/"+row.id).then((res)=>{
if(res.data.code == 20041){
//展示彈層,加載數(shù)據(jù)
this.formData = res.data.data;
this.dialogFormVisible4Edit = true;
}else{
this.$message.error(res.data.msg);
}
});
}
修改handleEdit
方法
handleEdit() {
//發(fā)送ajax請求
axios.put("/books",this.formData).then((res)=>{
//如果操作成功,關(guān)閉彈層,顯示數(shù)據(jù)
if(res.data.code == 20031){
this.dialogFormVisible4Edit = false;
this.$message.success("修改成功");
}else if(res.data.code == 20030){
this.$message.error("修改失敗");
}else{
this.$message.error(res.data.msg);
}
}).finally(()=>{
this.getAll();
});
}
至此修改功能就已經(jīng)完成。
6 刪除功能
需求:完成頁面的刪除功能。
1.找到頁面的刪除按鈕,按鈕上綁定了
@click="handleDelete(scope.row)"
2.method的
handleDelete
方法彈出提示框3.用戶點擊取消,提示操作已經(jīng)被取消。
4.用戶點擊確定,發(fā)送異步請求并攜帶需要刪除數(shù)據(jù)的主鍵ID
5.根據(jù)后臺返回結(jié)果做不同的操作
? 如果返回成功,提示成功信息,并重新查詢數(shù)據(jù)
? 如果返回失敗,提示錯誤信息,并重新查詢數(shù)據(jù)
修改handleDelete
方法
handleDelete(row) {
//1.彈出提示框
this.$confirm("此操作永久刪除當前數(shù)據(jù),是否繼續(xù)?","提示",{
type:'info'
}).then(()=>{
//2.做刪除業(yè)務(wù)
axios.delete("/books/"+row.id).then((res)=>{
if(res.data.code == 20021){
this.$message.success("刪除成功");
}else{
this.$message.error("刪除失敗");
}
}).finally(()=>{
this.getAll();
});
}).catch(()=>{
//3.取消刪除
this.$message.info("取消刪除操作");
});
}
接下來,下面是一個完整頁面
<!DOCTYPE html>
<html>
<head>
<!-- 頁面meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>SpringMVC案例</title>
<meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
<!-- 引入樣式 -->
<link rel="stylesheet" href="../plugins/elementui/index.css">
<link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
<div class="content-header">
<h1>圖書管理</h1>
</div>
<div class="app-container">
<div class="box">
<div class="filter-container">
<el-input placeholder="圖書名稱" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
<el-button @click="getAll()" class="dalfBut">查詢</el-button>
<el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
</div>
<el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
<el-table-column type="index" align="center" label="序號"></el-table-column>
<el-table-column prop="type" label="圖書類別" align="center"></el-table-column>
<el-table-column prop="name" label="圖書名稱" align="center"></el-table-column>
<el-table-column prop="description" label="描述" align="center"></el-table-column>
<el-table-column label="操作" align="center">
<template slot-scope="scope">
<el-button type="primary" size="mini" @click="handleUpdate(scope.row)">編輯</el-button>
<el-button type="danger" size="mini" @click="handleDelete(scope.row)">刪除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 新增標簽彈層 -->
<div class="add-form">
<el-dialog title="新增圖書" :visible.sync="dialogFormVisible">
<el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
<el-row>
<el-col :span="12">
<el-form-item label="圖書類別" prop="type">
<el-input v-model="formData.type"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="圖書名稱" prop="name">
<el-input v-model="formData.name"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="描述">
<el-input v-model="formData.description" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取消</el-button>
<el-button type="primary" @click="handleAdd()">確定</el-button>
</div>
</el-dialog>
</div>
<!-- 編輯標簽彈層 -->
<div class="add-form">
<el-dialog title="編輯檢查項" :visible.sync="dialogFormVisible4Edit">
<el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
<el-row>
<el-col :span="12">
<el-form-item label="圖書類別" prop="type">
<el-input v-model="formData.type"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="圖書名稱" prop="name">
<el-input v-model="formData.name"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="描述">
<el-input v-model="formData.description" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible4Edit = false">取消</el-button>
<el-button type="primary" @click="handleEdit()">確定</el-button>
</div>
</el-dialog>
</div>
</div>
</div>
</div>
</body>
<!-- 引入組件庫 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
var vue = new Vue({
el: '#app',
data:{
pagination: {},
dataList: [],//當前頁要展示的列表數(shù)據(jù)
formData: {},//表單數(shù)據(jù)
dialogFormVisible: false,//控制表單是否可見
dialogFormVisible4Edit:false,//編輯表單是否可見
rules: {//校驗規(guī)則
type: [{ required: true, message: '圖書類別為必填項', trigger: 'blur' }],
name: [{ required: true, message: '圖書名稱為必填項', trigger: 'blur' }]
}
},
//鉤子函數(shù),VUE對象初始化完成后自動執(zhí)行
created() {
this.getAll();
},
methods: {
//列表
getAll() {
//發(fā)送ajax請求
axios.get("/books").then((res)=>{
this.dataList = res.data.data;
});
},
//彈出添加窗口
handleCreate() {
this.dialogFormVisible = true;
this.resetForm();
},
//重置表單
resetForm() {
this.formData = {};
},
//添加
handleAdd () {
//發(fā)送ajax請求
axios.post("/books",this.formData).then((res)=>{
console.log(res.data);
//如果操作成功,關(guān)閉彈層,顯示數(shù)據(jù)
if(res.data.code == 20011){
this.dialogFormVisible = false;
this.$message.success("添加成功");
}else if(res.data.code == 20010){
this.$message.error("添加失敗");
}else{
this.$message.error(res.data.msg);
}
}).finally(()=>{
this.getAll();
});
},
//彈出編輯窗口
handleUpdate(row) {
// console.log(row); //row.id 查詢條件
//查詢數(shù)據(jù),根據(jù)id查詢
axios.get("/books/"+row.id).then((res)=>{
// console.log(res.data.data);
if(res.data.code == 20041){
//展示彈層,加載數(shù)據(jù)
this.formData = res.data.data;
this.dialogFormVisible4Edit = true;
}else{
this.$message.error(res.data.msg);
}
});
},
//編輯
handleEdit() {
//發(fā)送ajax請求
axios.put("/books",this.formData).then((res)=>{
//如果操作成功,關(guān)閉彈層,顯示數(shù)據(jù)
if(res.data.code == 20031){
this.dialogFormVisible4Edit = false;
this.$message.success("修改成功");
}else if(res.data.code == 20030){
this.$message.error("修改失敗");
}else{
this.$message.error(res.data.msg);
}
}).finally(()=>{
this.getAll();
});
},
// 刪除
handleDelete(row) {
//1.彈出提示框
this.$confirm("此操作永久刪除當前數(shù)據(jù),是否繼續(xù)?","提示",{
type:'info'
}).then(()=>{
//2.做刪除業(yè)務(wù)
axios.delete("/books/"+row.id).then((res)=>{
if(res.data.code == 20021){
this.$message.success("刪除成功");
}else{
this.$message.error("刪除失敗");
}
}).finally(()=>{
this.getAll();
});
}).catch(()=>{
//3.取消刪除
this.$message.info("取消刪除操作");
});
}
}
})
</script>
</html>
3,攔截器
對于攔截器這節(jié)的知識,我們需要學(xué)習(xí)如下內(nèi)容:
- 攔截器概念
- 入門案例
- 攔截器參數(shù)
- 攔截器工作流程分析
1. 攔截器概念
講解攔截器的概念之前,我們先看一張圖:
(1)瀏覽器發(fā)送一個請求會先到Tomcat的web服務(wù)器
(2)Tomcat服務(wù)器接收到請求以后,會去判斷請求的是靜態(tài)資源還是動態(tài)資源
(3)如果是靜態(tài)資源,會直接到Tomcat的項目部署目錄下去直接訪問
(4)如果是動態(tài)資源,就需要交給項目的后臺代碼進行處理
(5)在找到具體的方法之前,我們可以去配置過濾器(可以配置多個),按照順序進行執(zhí)行
(6)然后進入到到中央處理器(SpringMVC中的內(nèi)容),SpringMVC會根據(jù)配置的規(guī)則進行攔截
(7)如果滿足規(guī)則,則進行處理,找到其對應(yīng)的controller類中的方法進行執(zhí)行,完成后返回結(jié)果
(8)如果不滿足規(guī)則,則不進行處理
(9)這個時候,如果我們需要在每個Controller方法執(zhí)行的前后添加業(yè)務(wù),具體該如何來實現(xiàn)?
這個就是攔截器要做的事。
- 攔截器(Interceptor)是一種動態(tài)攔截方法調(diào)用的機制,在SpringMVC中動態(tài)攔截控制器方法的執(zhí)行
- 作用:
- 在指定的方法調(diào)用前后執(zhí)行預(yù)先設(shè)定的代碼
- 阻止原始方法的執(zhí)行
- 總結(jié):攔截器就是用來做增強
看完以后,大家會發(fā)現(xiàn)
- 攔截器和過濾器在作用和執(zhí)行順序上也很相似
所以這個時候,就有一個問題需要思考:攔截器和過濾器之間的區(qū)別是什么?
- 歸屬不同:Filter屬于Servlet技術(shù),Interceptor屬于SpringMVC技術(shù)
- 攔截內(nèi)容不同:Filter對所有訪問進行增強,Interceptor僅針對SpringMVC的訪問進行增強
2. 攔截器入門案例
1. 環(huán)境準備
-
創(chuàng)建一個Web的Maven項目
-
pom.xml添加SSM整合所需jar包
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>com.itheima</groupId> <artifactId>springmvc_12_interceptor</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.10.RELEASE</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <port>80</port> <path>/</path> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> </project>
-
創(chuàng)建對應(yīng)的配置類
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer { protected Class<?>[] getRootConfigClasses() { return new Class[0]; } protected Class<?>[] getServletConfigClasses() { return new Class[]{SpringMvcConfig.class}; } protected String[] getServletMappings() { return new String[]{"/"}; } //亂碼處理 @Override protected Filter[] getServletFilters() { CharacterEncodingFilter filter = new CharacterEncodingFilter(); filter.setEncoding("UTF-8"); return new Filter[]{filter}; } } @Configuration @ComponentScan({"com.itheima.controller"}) @EnableWebMvc public class SpringMvcConfig{ }
-
創(chuàng)建模型類Book
public class Book { private String name; private double price; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Book{" + "書名='" + name + '\'' + ", 價格=" + price + '}'; } }
-
編寫Controller
@RestController @RequestMapping("/books") public class BookController { @PostMapping public String save(@RequestBody Book book){ System.out.println("book save..." + book); return "{'module':'book save'}"; } @DeleteMapping("/{id}") public String delete(@PathVariable Integer id){ System.out.println("book delete..." + id); return "{'module':'book delete'}"; } @PutMapping public String update(@RequestBody Book book){ System.out.println("book update..."+book); return "{'module':'book update'}"; } @GetMapping("/{id}") public String getById(@PathVariable Integer id){ System.out.println("book getById..."+id); return "{'module':'book getById'}"; } @GetMapping public String getAll(){ System.out.println("book getAll..."); return "{'module':'book getAll'}"; } }
最終創(chuàng)建好的項目結(jié)構(gòu)如下:
2. 攔截器開發(fā)
步驟1:創(chuàng)建攔截器類
讓類實現(xiàn)HandlerInterceptor接口,重寫接口中的三個方法。
@Component
//定義攔截器類,實現(xiàn)HandlerInterceptor接口
//注意當前類必須受Spring容器控制
public class ProjectInterceptor implements HandlerInterceptor {
@Override
//原始方法調(diào)用前執(zhí)行的內(nèi)容
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle...");
return true;
}
@Override
//原始方法調(diào)用后執(zhí)行的內(nèi)容
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle...");
}
@Override
//原始方法調(diào)用完成后執(zhí)行的內(nèi)容
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion...");
}
}
注意: 攔截器類要被SpringMVC容器掃描到。
步驟2:配置攔截器類
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Autowired
private ProjectInterceptor projectInterceptor;
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
//配置攔截器
registry.addInterceptor(projectInterceptor).addPathPatterns("/books" );
}
}
步驟3:SpringMVC添加SpringMvcSupport包掃描
@Configuration
@ComponentScan({"com.itheima.controller","com.itheima.config"})
@EnableWebMvc
public class SpringMvcConfig{
}
步驟4:運行程序測試
使用PostMan發(fā)送http://localhost/books
如果發(fā)送http://localhost/books/100
會發(fā)現(xiàn)攔截器沒有被執(zhí)行,原因是攔截器的addPathPatterns
方法配置的攔截路徑是/books
,我們現(xiàn)在發(fā)送的是/books/100
,所以沒有匹配上,因此沒有攔截,攔截器就不會執(zhí)行。
步驟5:修改攔截器攔截規(guī)則
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Autowired
private ProjectInterceptor projectInterceptor;
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
//配置攔截器
registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*" );
}
}
這個時候,如果再次訪問http://localhost/books/100
,攔截器就會被執(zhí)行。
最后說一件事,就是攔截器中的preHandler
方法,如果返回true,則代表放行,會執(zhí)行原始Controller類中要請求的方法,如果返回false,則代表攔截,后面的就不會再執(zhí)行了。
步驟6:簡化SpringMvcSupport的編寫
@Configuration
@ComponentScan({"com.itheima.controller"})
@EnableWebMvc
//實現(xiàn)WebMvcConfigurer接口可以簡化開發(fā),但具有一定的侵入性
public class SpringMvcConfig implements WebMvcConfigurer {
@Autowired
private ProjectInterceptor projectInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//配置多攔截器
registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
}
}
此后咱們就不用再寫SpringMvcSupport
類了。
最后我們來看下攔截器的執(zhí)行流程:
當有攔截器后,請求會先進入preHandle方法,
? 如果方法返回true,則放行繼續(xù)執(zhí)行后面的handle[controller的方法]和后面的方法
? 如果返回false,則直接跳過后面方法的執(zhí)行。
3. 攔截器參數(shù)
1. 前置處理方法
原始方法之前運行preHandle
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("preHandle");
return true;
}
- request:請求對象
- response:響應(yīng)對象
- handler:被調(diào)用的處理器對象,本質(zhì)上是一個方法對象,對反射中的Method對象進行了再包裝
使用request對象可以獲取請求數(shù)據(jù)中的內(nèi)容,如獲取請求頭的Content-Type
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String contentType = request.getHeader("Content-Type");
System.out.println("preHandle..."+contentType);
return true;
}
使用handler參數(shù),可以獲取方法的相關(guān)信息
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod hm = (HandlerMethod)handler;
String methodName = hm.getMethod().getName();//可以獲取方法的名稱
System.out.println("preHandle..."+methodName);
return true;
}
2. 后置處理方法
原始方法運行后運行,如果原始方法被攔截,則不執(zhí)行
public void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
前三個參數(shù)和上面的是一致的。
modelAndView:如果處理器執(zhí)行完成具有返回結(jié)果,可以讀取到對應(yīng)數(shù)據(jù)與頁面信息,并進行調(diào)整
因為咱們現(xiàn)在都是返回json數(shù)據(jù),所以該參數(shù)的使用率不高。
3. 完成處理方法
攔截器最后執(zhí)行的方法,無論原始方法是否執(zhí)行
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) throws Exception {
System.out.println("afterCompletion");
}
前三個參數(shù)與上面的是一致的。
ex:如果處理器執(zhí)行過程中出現(xiàn)異常對象,可以針對異常情況進行單獨處理
因為我們現(xiàn)在已經(jīng)有全局異常處理器類,所以該參數(shù)的使用率也不高。
這三個方法中,最常用的是preHandle,在這個方法中可以通過返回值來決定是否要進行放行,我們可以把業(yè)務(wù)邏輯放在該方法中,如果滿足業(yè)務(wù)則返回true放行,不滿足則返回false攔截。
4. 攔截器鏈配置
目前,我們在項目中只添加了一個攔截器,如果有多個,該如何配置?配置多個后,執(zhí)行順序是什么?
1. 配置多個攔截器
步驟1:創(chuàng)建攔截器類
實現(xiàn)接口,并重寫接口中的方法
@Component
public class ProjectInterceptor2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle...222");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle...222");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion...222");
}
}
步驟2:配置攔截器類
@Configuration
@ComponentScan({"com.itheima.controller"})
@EnableWebMvc
//實現(xiàn)WebMvcConfigurer接口可以簡化開發(fā),但具有一定的侵入性
public class SpringMvcConfig implements WebMvcConfigurer {
@Autowired
private ProjectInterceptor projectInterceptor;
@Autowired
private ProjectInterceptor2 projectInterceptor2;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//配置多攔截器
registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
registry.addInterceptor(projectInterceptor2).addPathPatterns("/books","/books/*");
}
}
步驟3:運行程序,觀察順序
攔截器執(zhí)行的順序是和配置順序有關(guān)。就和前面所提到的運維人員進入機房的案例,先進后出。
- 當配置多個攔截器時,形成攔截器鏈
- 攔截器鏈的運行順序參照攔截器添加順序為準
- 當攔截器中出現(xiàn)對原始處理器的攔截,后面的攔截器均終止運行
- 當攔截器運行中斷,僅運行配置在前面的攔截器的afterCompletion操作
preHandle:與配置順序相同,必定運行
postHandle:與配置順序相反,可能不運行
afterCompletion:與配置順序相反,可能不運行。
這個順序不太好記,最終只需要把握住一個原則即可:以最終的運行結(jié)果為準
筆記來自:黑馬程序員SSM框架教程
4,圖書推薦
本書專門為想要入行從事產(chǎn)品經(jīng)理相關(guān)工作的零基礎(chǔ)就業(yè)人員而編寫,涵蓋了在產(chǎn)品準備階段、產(chǎn)品開發(fā)階段、產(chǎn)品測試階段和產(chǎn)品上線后產(chǎn)品經(jīng)理所需要具備的各項基本技能。
全書共分為9章,第1章大致認識產(chǎn)品經(jīng)理的職能范圍;第2章講解了產(chǎn)品經(jīng)理的工作職責(zé);第3章講解了需求的相關(guān)知識;第4章介紹了流程的相關(guān)知識;第5章展示了結(jié)構(gòu)圖;第6章詳細闡述了界面原型的相關(guān)知識;第7章介紹了UML圖;第8章介紹了PRD文檔的相關(guān)知識;第9章介紹了軟件測試的相關(guān)知識。
本書適合零基礎(chǔ)想要入門產(chǎn)品經(jīng)理、產(chǎn)品規(guī)劃師、需求分析師的人員閱讀,也適合用戶體驗、市場運營等相關(guān)部門的朋友,特別是互聯(lián)網(wǎng)、軟件行業(yè)、AI開發(fā)行業(yè)的人員閱讀,還適合大專院?;ヂ?lián)網(wǎng)和產(chǎn)品開發(fā)相關(guān)專業(yè)的老師和學(xué)生閱讀。
??本次送 5本書 ,從評論區(qū)抽5位小伙伴??
活動時間:截止到 2023-06-23 20:00:00
抽獎方式:利用程序進行抽獎。
參與方式:關(guān)注博主、點贊、收藏,進行優(yōu)質(zhì)評論文章來源:http://www.zghlxwxcb.cn/news/detail-490838.html
?? 獲獎名單??
釉色清風(fēng)
在努力的前端小白
冰.封萬里
韓楚風(fēng)
萬物皆可der
名單公布時間: 2023-06-23 20:00:00文章來源地址http://www.zghlxwxcb.cn/news/detail-490838.html
到了這里,關(guān)于【SpringMVC】統(tǒng)一異常處理 前后臺協(xié)議聯(lián)調(diào) 攔截器(文末贈書)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!