在 Java Play Framework 2.2.x 中启用 CORS [英] Enable CORS in Java Play Framework 2.2.x

查看:25
本文介绍了在 Java Play Framework 2.2.x 中启用 CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 Java Play 2.2.x 中启用跨域

I am having trouble of enabling cross domain in Java Play 2.2.x

在 Java Play 2.1.3 中,此代码通过将其放入 Global.java 来工作

In Java Play 2.1.3 this code works by putting it in 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 上编译时,它不再编译了.

But when I tried to compile on java play 2.2.x, it does not compile anymore.

编译错误信息:

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

Global.ActionWrapper is not abstract and does not override abstract method call(Context) in Action ...

java play 2.2.x 有没有等价的代码?

Is there any equivalent code for 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 Play Framework 2.2.x 中启用 CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆