Java :重写的方法不会抛出 'java.lang.Throwable'

Java : Overridden method does not throw 'java.lang.Throwable'

我遇到以下编译时错误

call(play.mvc.Http.Context) in actions.Headers cannot override call(play.mvc.Http.Context) in play.mvc.Action
[error]   overridden method does not throw java.lang.Exception
[error]        public CompletionStage<Result> call(final Http.Context ctx) throws Throwable {

这是代码:

public CompletionStage<Result> call(final Http.Context ctx) throws Throwable {
           ctx.response().setHeader("Access-Control-Allow-Origin", "*");
           ctx.response().setHeader("Allow", "*");
           ctx.response().setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
           ctx.response().setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Referer, User-Agent, token");
           ctx.response().setHeader("Access-Control-Expose-Headers", "Origin, X-Requested-With, Content-Type, Accept, Referer, User-Agent, token");

           return (CompletionStage<Result>) delegate.call(ctx);
       }

我正在玩 framework 2.6

只需从您的方法声明中删除 [throws Throwable]。

根据 play framework 2.6 的 documentation,class Action 中的方法调用不再抛出 Throwable,因此您的方法也不能那样做。