aragakiyuii
V2EX  ›  Java

关于 Spring @ControllerAdvice 官方文档翻译问题

  •  
  •   aragakiyuii · Jun 29, 2020 · 2219 views
    This topic created in 2157 days ago, the information mentioned may be changed or developed.

    原文: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-error-handling

    原文中有这么一段

    其中:

    if YourException is thrown by a controller defined in the same package as AcmeController

    这个 defined 修饰的是 controller 还是 YourException 。。?

    假如我有个 a,b,c 三个 module, a 和 b 都依赖 c. c 中有一个异常 CException

    public class CException extends RuntimeException {}
    

    分别在 a 和 b 中的某个 controller 抛出, 并且 a 和 b 中分别定义了 ControllerAdvice

    @ControllerAdvice(basePackageClasses = AController.class)
    public class AControllerAdvice {
    
        @ExceptionHandler(CException.class)
        public ResponseEntity handleCException() {
        	// ...
        }
    }
    
    @ControllerAdvice(basePackageClasses = BController.class)
    public class BControllerAdvice {
    
        @ExceptionHandler(CException.class)
        public ResponseEntity handleCException() {
        	// ...
        }
    }
    

    哪个 advice 能拦截到? 还是说要根据 order 来判断..?

    hello2060
        1
    hello2060  
       Jun 29, 2020 via iPhone
    没看 code,光说英语 defined 当然指的是 controller
    zsdroid
        2
    zsdroid  
       Jun 29, 2020   ❤️ 1
    访问 A 的 controller 报 CException 错就是 A 的 ControllerAdvice 啊
    访问 B 的 controller 报 CException 错就是 B 的 ControllerAdvice 啊
    zsdroid
        3
    zsdroid  
       Jun 29, 2020
    再不济,创建的 demo 试一下。现在 idea 创建 spring-demo 快的一笔
    siweipancc
        4
    siweipancc  
       Jun 29, 2020 via iPhone
    (・・;) 这不是单纯英语语法的问题吗
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3459 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 10:43 · PVG 18:43 · LAX 03:43 · JFK 06:43
    ♥ Do have faith in what you're doing.