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

查看:627
本文介绍了在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播放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天全站免登陆