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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Spring MVC 異常處理方式

當(dāng)程序發(fā)生異常時(shí)我們可以通過如下兩個(gè)注解來統(tǒng)一處理異常信息。

創(chuàng)新互聯(lián)建站專注于網(wǎng)站建設(shè),為客戶提供做網(wǎng)站、成都做網(wǎng)站、網(wǎng)頁設(shè)計(jì)開發(fā)服務(wù),多年建網(wǎng)站服務(wù)經(jīng)驗(yàn),各類網(wǎng)站都可以開發(fā),品牌網(wǎng)站設(shè)計(jì),公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設(shè)計(jì),建網(wǎng)站費(fèi)用,建網(wǎng)站多少錢,價(jià)格優(yōu)惠,收費(fèi)合理。

@ControllerAdvice 和 @RestControllerAdvice

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

源碼:

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

方式一、Controller內(nèi)部處理異常

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

這樣如果這個(gè)Controller中的接口發(fā)生了異常那么就會(huì)執(zhí)行有@ExceptionHandler標(biāo)注的方法。

該種方式處理異常只是針對(duì)當(dāng)前Controller,一個(gè)項(xiàng)目肯定會(huì)有很多的Controller,如果每一個(gè)類都這樣處理明顯是太麻煩,而且還不方便統(tǒng)一異常的處理。

方式二、全局異常處理

可以在一個(gè)類上添加 @RestControllerAdvice或@ControlerAdvice

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

到此全局異常的使用方式就結(jié)束了當(dāng)你訪問接口時(shí)你會(huì)發(fā)現(xiàn)全局異常沒有起作用。

當(dāng)我們把controller中的@ExceptionHandler注釋了,這時(shí)全局異常才會(huì)生效。

結(jié)論:局部異常處理優(yōu)先級(jí)高于全局異常處理。

以上是項(xiàng)目中如果使用異常處理句柄的方式;接下來我們來看看在全局異常處理句柄中如何進(jìn)行局部控制(比如只處理有特定注解的或是只處理部分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("發(fā)生錯(cuò)誤了") ;
    }
    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發(fā)生錯(cuò)誤了") ;
    }
    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會(huì)被處理。

2、只處理指定的Controller

新建UserController

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

新建PersonController

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


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

全局異常處理類:

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

這里通過assignableTypes屬性來限定了只有UserController類發(fā)生了異常才會(huì)做出響應(yīng)。

PersonController發(fā)生異常不會(huì)被處理。

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錯(cuò)誤") ;
    }
    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錯(cuò)誤") ;
    }


    return "Person" ;
  }}

當(dāng)訪問com.pack.pkg1包下的接口出現(xiàn)異常后就會(huì)被處理。

到此結(jié)束

關(guān)于@ExceptionHandler 方法句柄可接受的參數(shù)及返回值大家可參考這里

接受的參數(shù)類型


網(wǎng)站欄目:Spring MVC 異常處理方式
當(dāng)前URL:http://m.5511xx.com/article/coedopi.html