I am having trouble of enabling cross domain in Java Play 2.2.x
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));
}
}
But when I tried to compile on java play 2.2.x, it does not compile anymore.
The compilation error message:
Global.ActionWrapper is not abstract and does not override abstract method call(Context) in Action ...
Is there any equivalent code for java play 2.2.x?
Thanks.
It looks like this:
The solutions proposed by @alexhanschke does not work when the request throws an exception (internal server error), because filters are not applied when that happens (see https://github.com/playframework/playframework/issues/2429). To solve that you have to wrap a scala class and return that as a result, as shown below in full. Please note that this still requires the options route specified and a controller to handle the options request.
See the entire thing here https://gist.github.com/tinusn/38c4c110f7cd1e1ec63f.
Another option might be using Filters. Currently there are only Scala filters available. However, as pointed out in this post for the sake of simply modifying the response headers you can copy&paste the following to enable CORS.
And then add the filter in your
Global.java
just so:For anyone using 2.3.1+ (as of this writing) of Play, it's now
Promise<Result>
instead ofPromise<SimpleResult>