日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
為你Springboot項目自定義一個通用的異常

前言

我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、石柱土家族ssl等。為超過千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的石柱土家族網(wǎng)站制作公司

我們的項目通常來講都是一個比較大的項目,包含了各種各樣的服務(wù)。如果每個服務(wù)都以不同的方式返回異常信息,這樣排查的時候就會比較凌亂。如果我們定義一個標(biāo)準(zhǔn)的異常處理體系。并在所有的服務(wù)中使用。那樣開發(fā)起來就可以快速定位。頁面也會更加的簡單和直觀。

本文開發(fā)環(huán)境基于springboot2.4,IDE環(huán)境是IDEA。這里從一個最簡單的異常案例。逐步過渡到完全自定義自己的異常。

案例:Springboot查詢數(shù)據(jù)庫數(shù)據(jù),發(fā)現(xiàn)返回的是null,就拋出異常。

OK,基于這個思想,看一下實現(xiàn)的思路。

一、簡單案例代碼實現(xiàn)

1、新建一個Springboot應(yīng)用

2、新建dao包,創(chuàng)建User類

這個比較簡單,代碼如下:

 
 
 
 
  1. public class User { 
  2.     private int id; 
  3.     private String name; 
  4.     public User() { 
  5.     } 
  6.     public User(int id, String name) { 
  7.         this.id = id; 
  8.         this.name = name; 
  9.     } 
  10.     //getter和setter方法 
  11.     //toString方法 

3、新建service包,創(chuàng)建UserService

 
 
 
 
  1. @Service 
  2. public class UserService { 
  3.     public User findOne(int id){ 
  4.         //本來應(yīng)該向數(shù)據(jù)庫查詢User,但是數(shù)據(jù)庫沒有 
  5.         return null; 
  6.     } 

由于演示的是異常的案例,因此這里沒有真正實現(xiàn)數(shù)據(jù)庫的增刪改查操作。當(dāng)調(diào)用findOne方法時,直接返回為null即可。

4、新建controller包,創(chuàng)建UserController類

 
 
 
 
  1. @RestController 
  2. public class UserController { 
  3.     @Autowired 
  4.     private UserService service; 
  5.     @GetMapping("/users/{id}") 
  6.     public User retriveUser(@PathVariable int id)  
  7.             throws UserNotFoundException { 
  8.         User user= service.findOne(id); 
  9.         if(user==null) 
  10.             throw new UserNotFoundException("id: "+ id); 
  11.         return user; 
  12.     } 

這里自定義了一個異常UserNotFoundException,當(dāng)數(shù)據(jù)庫查詢的時候一旦發(fā)現(xiàn)返回值為null,就直接拋出這個異常。

5、在controller包下,創(chuàng)建UserNotFoundException類

 
 
 
 
  1. public class UserNotFoundException extends RuntimeException { 
  2.     public UserNotFoundException(String message){ 
  3.         super(message); 
  4.         System.out.println("異常信息是:"+message); 
  5.     } 

6、postman測試

這時候進行測試會發(fā)現(xiàn)服務(wù)器代碼會報錯。我們的資源沒有找到總不能提示服務(wù)器內(nèi)部錯誤吧?,F(xiàn)在對拋出的異常進行一個處理。

7、異常處理

 
 
 
 
  1. @ResponseStatus(HttpStatus.NOT_FOUND) 
  2. public class UserNotFoundException extends RuntimeException { 
  3.     public UserNotFoundException(String message){ 
  4.         super(message); 
  5.         System.out.println("異常信息是:"+message); 
  6.     } 

我們將添加一個注釋@ResponseStatus來生成狀態(tài):404 Not Found。當(dāng)然還有其他的狀態(tài)。這個可以根據(jù)自己的需要去返回。我們使用了HttpStatus.NOT_FOUND用戶訪問的時候,一旦拋出了異常就會顯示404錯誤。這個你換成其他的狀態(tài),還會顯示其他的信息。

8、重新測試

Spring Boot和Spring MVC框架的結(jié)合提供了錯誤處理。其內(nèi)部已經(jīng)自動配置一些默認(rèn)異常處理。所以在開發(fā)中為所有服務(wù)配置一致的異常消息是很重要的。

二、通用的異常處理

1、添加依賴

 
 
 
 
  1.  
  2.       org.springframework 
  3.       spring-webmvc 
  4.       5.3.2 
  5.  

2、創(chuàng)建異常返回實體類ExceptionResponse

這個類的作用是,當(dāng)有異常時,我們想要展示的信息。

 
 
 
 
  1. public class ExceptionResponse { 
  2.     private Date timestamp; 
  3.     private String message; 
  4.     private String detail; 
  5.     public ExceptionResponse() { } 
  6.     public ExceptionResponse(Date timestamp, String message, String detail) { 
  7.         this.timestamp = timestamp; 
  8.         this.message = message; 
  9.         this.detail = detail; 
  10.     } 
  11.     public Date getTimestamp() { 
  12.         return timestamp; 
  13.     } 
  14.     public String getMessage() { 
  15.         return message; 
  16.     } 
  17.     public String getDetail() { 
  18.         return detail; 
  19.     } 

這里只需要實現(xiàn)getter方法,setter方法就不需要。

3、創(chuàng)建通用異常處理類

 
 
 
 
  1. @ControllerAdvice 
  2. @RestController 
  3. public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { 
  4.  
  5.     //此方法主要處理所有的異常信息 
  6.     @ExceptionHandler(Exception.class) 
  7.     public final ResponseEntity handleAllExceptions(Exception ex, WebRequest request) { 
  8.         //當(dāng)出現(xiàn)異常時,我們輸出的信息,這里被封裝在了ExceptionResponse 
  9.         ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false)); 
  10.         return new ResponseEntity(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR); 
  11.     } 
  12.     //當(dāng)頁面資源沒有找到時,拋出的異常 
  13.     @ExceptionHandler(UserNotFoundException.class) 
  14.     public final ResponseEntity handleUserNotFoundExceptions(UserNotFoundException ex, WebRequest request) { 
  15.         ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false)); 
  16.         return new ResponseEntity(exceptionResponse, HttpStatus.NOT_FOUND); 
  17.     } 
  18. 很簡單。里面有很多API,可以自己根據(jù)需要去查即可。

    4、postman測試

    萬事大吉。趕快為你的程序自定義一個通用的異常處理程序吧。

    本文轉(zhuǎn)載自微信公眾號「愚公要移山」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系愚公要移山公眾號。


    當(dāng)前題目:為你Springboot項目自定義一個通用的異常
    網(wǎng)站鏈接:http://m.5511xx.com/article/dpscegc.html