gpt4 book ai didi

java - 在 Java Play Framework 2.2.x 中启用 CORS

转载 作者:搜寻专家 更新时间:2023-10-30 19:57:52 25 4
gpt4 key购买 nike

我在 Java Play 2.2.x 中启用跨域时遇到问题

在 Java Play 2.1.3 中,这段代码通过将它放在 Global.java 中来工作

public class Global extends GlobalSettings {

private class ActionWrapper extends Action.Simple {
public ActionWrapper(Action action) {
this.delegate = action;
}

@Override
public Result call(Http.Context ctx) throws java.lang.Throwable {
Result result = this.delegate.call(ctx);
Http.Response response = ctx.response();
response.setHeader("Access-Control-Allow-Origin", "*");
return result;
}
}

@Override
public Action onRequest(Http.Request request, java.lang.reflect.Method actionMethod) {
return new ActionWrapper(super.onRequest(request, actionMethod));
}

}

但是当我尝试在 java play 2.2.x 上编译时,它不再编译了。

编译错误信息:

Global.ActionWrapper 不是抽象的,不会覆盖 Action 中的抽象方法调用(Context) ...

是否有 java play 2.2.x 的等效代码?

谢谢。

最佳答案

看起来像这样:

import play.GlobalSettings;
import play.libs.F.Promise;
import play.mvc.Action;
import play.mvc.Http;
import play.mvc.SimpleResult;

public class Global extends GlobalSettings {
private class ActionWrapper extends Action.Simple {
public ActionWrapper(Action<?> action) {
this.delegate = action;
}

@Override
public Promise<SimpleResult> call(Http.Context ctx) throws java.lang.Throwable {
Promise<SimpleResult> result = this.delegate.call(ctx);
Http.Response response = ctx.response();
response.setHeader("Access-Control-Allow-Origin", "*");
return result;
}
}

@Override
public Action<?> onRequest(Http.Request request, java.lang.reflect.Method actionMethod) {
return new ActionWrapper(super.onRequest(request, actionMethod));
}
}

关于java - 在 Java Play Framework 2.2.x 中启用 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22144788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com