0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

playframework2.6(java)Migration/requestHandler => actionCreator

Last updated at Posted at 2019-11-27

play2.4からplay2.6にMigration中

既存アプリケーションでは「Java Http Request Handlers - 2.4.x」の要領で、play.http.HttpRequestHandlercreateActionをOverrideしてすべてのリクエストをフックして、リクエストのログ出力等を行っています。

Play2.6にMigrationしたところこの部分でコンパイルエラーが発生しました。

HttpRequestHandlerからcreateActionがなくなって、Implements対象はhandlerForRequestとなっています。ところが、handlerForRequestのパラメタは以下の通りRequestでなくRequestHeaderとなっていて必要な情報(例えばAjax/POSTリクエストのbody)が取得できません。

@Override
public HandlerForRequest handlerForRequest(RequestHeader header)

困惑していたらcreateActionplay.http.ActionCreatorというクラスに定義が移動しているようで、これをImplementsしてみました。

変更点は、廃止されたPlay独自定義のPromiseをJava標準のCompletionStageに置きかえるだけです。(logRequestやupdateMenuItemは独自定義メソッドです)

play2.4/createAction
  @Override
  public Action createAction(Request request, Method method) {
    logRequest(request, method);
    try {
      return new Action.Simple() {
        @Override
        public Promise<Result> call(Http.Context context) throws Throwable {
          updateMenuItem(request, context);
          return delegate.call(context);
        }
      };
    } finally {
    }
  }
play2.6/createAction
  @Override
  public Action createAction(Request request, Method method) {
    logRequest(request, method);
    try {
      return new Action.Simple() {
        @Override
        public CompletionStage<Result> call(Http.Context context) {
          updateMenuItem(request, context);
          return delegate.call(context);
        }
      };
    } finally {
    }
  }

これでOKかと思って、アプリケーションを実行したら以下のエラーが発生しました。

Cannot load play.http.requestHandler
play.http.requestHandler [class java.lang.Class}] does not implement interface 
play.api.http.HttpRequestHandler or interface play.http.HttpRequestHandler.

色々と調べてみたところ、application.confで当該クラスを紐付ける対象を変更して解決。
これで既存と同じ動きを実現できました。

前)play.http.requestHandler = "com.nyango.xxx.xxx.xxx.httpRequestHadler"
後)play.http.actionCreator = "com.nyango.xxx.xxx.xxx.httpRequestHadler"

公式のMigration手順にも記載されておらず、そこそこ時間を要しました。
参考になれば幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?