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 1 year has passed since last update.

ktorのSessionsでio.ktor.application.MissingApplicationFeatureException: Application feature Sessions is not installeというエラーが出る

Last updated at Posted at 2022-12-22

はじめに

以下の書籍をやっていたところつまづいたので解決方法をまとめます

Ktorを使う上での常識ですが、使ったことのなかった私にとって解決するのもひとくろうでし

問題

書籍通りの以下のコードでエラーが発生した

Application.kt
        get("/board") {
            val session = call.sessions.get<MySession>() ?: MySession(null, null)
io.ktor.application.MissingApplicationFeatureException: Application feature Sessions is not installed

解決方法

この記事をみてKtorの使い方について理解した

Ktorでは認証、ルーティング、ヘッダー付与、Cookie、テンプレートエンジン等の機能は全て、Featureと呼ばれるプラグインになっており、Application.installメソッドでそれらを利用可能にする。

追加の機能はinstallメソッドを利用してプラグインの有効化しないといけないらしいです。たしかにmustacheはinstallしてました(書籍にはこの部分がないです)

ということで以下のようにApplicationモジュール実行時にinstallしました

Application.kt
fun Application.module(testing: Boolean = false) {
    install(Sessions)

ちなみにここまででエラーは消えるのですが書籍のプログラムでは別のエラーが発生しますので以下の記事を参考にしてください

おわりに

書籍通りにやっても動かないのではと思って、サンプルコードを見に行きましたが最後のChapter7-5だけサンプルコードがないという感じでなかなか辛かったです

参考

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?