成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring MVC 異常處理方式

開發 前端
他們的區別其實就是Rest的注解中多了一個@ResponseBody 注解(將方法的返回值,以特定的格式寫入到response的body,進而將數據返回給客戶端,如果是字符串直接輸出字符串信息,如果是對象將會把對象轉為json進行輸出)。

當程序發生異常時我們可以通過如下兩個注解來統一處理異常信息。

@ControllerAdvice 和 @RestControllerAdvice

他們的區別其實就是Rest的注解中多了一個@ResponseBody 注解(將方法的返回值,以特定的格式寫入到response的body,進而將數據返回給客戶端,如果是字符串直接輸出字符串信息,如果是對象將會把對象轉為json進行輸出)。

源碼:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ControllerAdvice
@ResponseBody
public @interface RestControllerAdvice {
}

方式一、Controller內部處理異常

@RestController
public class TestController {  
  @GetMapping("/test/{id}")
  public Object test(@PathVariable Integer id) {
    if (id < 5) {
      throw new RuntimeException("運行時異常") ;
    }
    return "測試異常處理" ;
  }
  
  @ExceptionHandler
  public Object handle(Exception e) {
    return e.getMessage() ;
  }
  
}

這樣如果這個Controller中的接口發生了異常那么就會執行有@ExceptionHandler標注的方法。

該種方式處理異常只是針對當前Controller,一個項目肯定會有很多的Controller,如果每一個類都這樣處理明顯是太麻煩,而且還不方便統一異常的處理。

方式二、全局異常處理

可以在一個類上添加 @RestControllerAdvice或@ControlerAdvice

@RestControllerAdvice
public class TestControllerAdvice {
  @ExceptionHandler
  public Object handle(Exception e) {
    return "我是全局異常:" + e.getMessage() ;
  }  
}

到此全局異常的使用方式就結束了當你訪問接口時你會發現全局異常沒有起作用。

當我們把controller中的@ExceptionHandler注釋了,這時全局異常才會生效。

結論:局部異常處理優先級高于全局異常處理。

以上是項目中如果使用異常處理句柄的方式;接下來我們來看看在全局異常處理句柄中如何進行局部控制(比如只處理有特定注解的或是只處理部分controller又或者是指定包下的controller)。

1、只處理特定注解

自定義Annotation:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AppAnnotation {


}

Controller類:

有@AppAnnotation注解的Controller

@AppAnnotation
@RestController
public class AnnotationController {


  @GetMapping("/an/get/{id}")
  public Object an(@PathVariable Integer id) {
    if (id < 10) {
      throw new RuntimeException("發生錯誤了") ;
    }
    return "自定義Annotation注解: " + id ;
  }
  
}

沒有@AppAnnotation注解的Controller

@RestController
public class AnnotationController2 {
  
  @GetMapping("/an/get2/{id}")
  public Object an(@PathVariable Integer id) {
    if (id < 10) {
      throw new RuntimeException("2發生錯誤了") ;
    }
    return "自定義Annotation注解2: " + id ;
  }
}

ControllerAdvice異常處理類:

@RestControllerAdvice(annotations = {AppAnnotation.class})
public class AnnotationControllerAdvice {
  
  @ExceptionHandler
  public Object handle(Exception e) {
    return "特定注解全局異常:" + e.getMessage() ;
  }
  
}

分別訪問/an/get/1 和/an/get2/1接口,只有有@AppAnnotation注解的Controller會被處理。

2、只處理指定的Controller

新建UserController

@RestController
public class UserController {
    
  @GetMapping("/user/{id}")
  public Object get(@PathVariable Integer id) {
    if (id < 10) {
      throw new RuntimeException("用戶ID錯誤") ;
    }
    return "Users" ;
  }
  
}

新建PersonController

@RestController
public class PersonController {
  
  
  @GetMapping("/person/{id}")


  public Object get(@PathVariable Integer id) {
    if (id < 10) {      throw new RuntimeException("Person ID錯誤") ;
    }
    return "Person" ;
  }
  
}

全局異常處理類:

@RestControllerAdvice(assignableTypes = {UserController.class})
public class SpecificControllerAdvice {
  
  @ExceptionHandler
  public Object handle(Exception e) {
    return "指定Controller全局異常:" + e.getMessage() ;
  }
  
}

這里通過assignableTypes屬性來限定了只有UserController類發生了異常才會做出響應。

PersonController發生異常不會被處理。

3、指定包下的Controller

@RestControllerAdvice(basePackages = {"com.pack.pkg1"})
public class PackageControllerAdvice {
  
  @ExceptionHandler
  public Object handle(Exception e) {
    return "指定包下的全局異常:" + e.getMessage() ;
  }
  
}

UserController類位于pkg1包下:

package com.pack.pkg1;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController("userPController")
public class UserController {
  @GetMapping("/userp/{id}")
  public Object get(@PathVariable Integer id) {
    if (id < 10) {
      throw new RuntimeException("用戶ID錯誤") ;
    }
    return "Users" ;
  }
}

PersonController類位于pkg2包下:

package com.pack.pkg2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController("personPController")
public class PersonController {
  @GetMapping("/personp/{id}")
  public Object get(@PathVariable Integer id) {
    if (id < 10) {
      throw new RuntimeException("Person ID錯誤") ;
    }


    return "Person" ;
  }}

當訪問com.pack.pkg1包下的接口出現異常后就會被處理。

到此結束

關于@ExceptionHandler 方法句柄可接受的參數及返回值大家可參考這里


圖片

接受的參數類型


圖片

責任編輯:武曉燕 來源: 實戰案例錦集
相關推薦

2023-08-25 08:35:58

Rest方法字符串

2021-03-31 09:11:27

URLErrorHTTPError

2011-05-24 09:22:44

Spring3異常處理

2024-12-18 16:19:51

2023-09-29 11:29:12

Spring異常處理類

2018-08-14 13:26:07

異常設計斷網

2025-01-26 00:00:25

限流組件HTTP

2017-04-17 10:05:51

Hadoop錯誤方式

2009-12-31 14:25:19

Silverlight

2010-01-18 16:58:29

VB.NET Over

2023-02-23 08:15:33

Spring異常處理機制

2009-07-21 15:47:35

JDBC批處理

2023-10-08 20:31:18

React

2022-03-07 14:39:01

前端框架批處理

2024-04-07 08:50:00

GenAIAI人工智能

2010-01-13 17:23:36

VB.NET動態事件

2010-01-07 17:57:22

VB.NET構造函數

2024-11-20 13:20:32

2025-01-27 12:19:51

2009-06-12 08:57:56

Unix字符串處理
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲综合精品 | 成人在线一区二区 | 国产在线中文字幕 | 9porny九色视频自拍 | 99热首页 | 成人高清视频在线观看 | 97久久精品午夜一区二区 | 欧美日韩高清免费 | 欧美日韩精品在线免费观看 | 国产成人久久精品一区二区三区 | 伊人天堂网| 一级a性色生活片久久毛片 午夜精品在线观看 | 中文字幕视频在线观看 | 午夜噜噜噜 | 亚洲va在线va天堂va狼色在线 | 日本亚洲一区 | 99精品久久 | 狠狠操电影 | 美女国产 | 一区二区三区四区不卡 | 日韩欧美一区二区三区在线播放 | 免费在线日韩 | 国产高清在线精品 | 亚洲一区二区在线播放 | 国产综合精品 | 免费视频一区二区三区在线观看 | 91视频三区| 久久国产亚洲 | 91成人在线视频 | 亚洲自拍偷拍视频 | 欧美综合一区二区三区 | 国产二区在线播放 | аⅴ资源新版在线天堂 | 毛片大全 | 亚洲激情在线观看 | 久久高清 | 日本粉嫩一区二区三区视频 | 欧美一区二区网站 | 国产精品视频在线播放 | 自拍偷拍亚洲欧美 | 久久精点视频 |