Atropo
Atropo

Reputation: 12541

Action composition using @With annotation in Play framework (Java)

How can I use two action compositions in Play Framework 2.4 (in Java)?

Suppose that, to avoid code duplication, I've got two actions to use :Auth and LogData.

How can I use both in an action composition?

This won't compile, causing a duplicate annotation error:

# play.PlayExceptions$CompilationException: Compilation error[error: duplicate annotation]

 @play.db.jpa.Transactional()
        @With(Auth.class)
        @With(LogData.class)
        public static Result callForumTeacher(String random, Long gameId){
               //Action code 
               return ok(Json.toJson("data"));
        }

This is a skeleton on how Auth and LogData are implemented:

public class CheckPausedGame extends Action.Simple {

     @Override
        public F.Promise<Result> call(Http.Context context) throws Throwable {
            if (checkCondition(context)) {
                return delegate.call(context);
            } else {
                F.Promise<Result> promise = F.Promise.promise(new F.Function0<Result>() {
                    @Override
                    public Result apply() throws Throwable {
                        return redirect("/paused");
                    }
                });
                return promise;
            }
        }
    }

This only a skeleton omitting some methods not useful for this question.

Upvotes: 1

Views: 772

Answers (1)

Cubic
Cubic

Reputation: 15683

While the documentation doesn't seem to clearly state this (at least I haven't found it anywhere), the intended way to use @With in cases like this is to pass all Actions at once (With takes an array)

Your code becomes

@play.db.jpa.Transactional()
        @With(value = {Auth.class, LogData.class})
        public static Result callForumTeacher(String random, Long gameId){
               //Action code 
               return ok(Json.toJson("data"));
        }

See the api doc

Upvotes: 2

Related Questions