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

2020-06-29 15:41:34 +08:00
 aragakiyuii

原文: 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 来判断..?

1746 次点击
所在节点    Java
4 条回复
hello2060
2020-06-29 16:26:41 +08:00
没看 code,光说英语 defined 当然指的是 controller
zsdroid
2020-06-29 17:01:39 +08:00
访问 A 的 controller 报 CException 错就是 A 的 ControllerAdvice 啊
访问 B 的 controller 报 CException 错就是 B 的 ControllerAdvice 啊
zsdroid
2020-06-29 17:05:28 +08:00
再不济,创建的 demo 试一下。现在 idea 创建 spring-demo 快的一笔
siweipancc
2020-06-29 20:51:54 +08:00
(・・;) 这不是单纯英语语法的问题吗

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/685627

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX