多个ControllerAdvice优先级设定

  • Post author:
  • Post category:其他


多个ControllerAdvice,优先级由@Order决定,order的数值越小 则优先级越高。或者,实现Ordered接口,实现getOrder()方法。

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
@ControllerAdvice 
@Order(Ordered.HIGHEST_PRECEDENCE) 
class UserProfileExceptionHandler { 
    @ExceptionHandler(UserProfileException) 
    @ResponseBody 
    ResponseEntity<ErrorResponse> handleUserProfileException() { 
     .... 
    } 
} 

@ControllerAdvice 
@Order(Ordered.LOWEST_PRECEDENCE) 
class DefaultExceptionHandler { 

    @ExceptionHandler(RuntimeException) 
    @ResponseBody 
    ResponseEntity<ErrorResponse> handleRuntimeException() { 
     .... 
    } 
} 



版权声明:本文为gaoshan12345678910原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。