1
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でContentNegotiationがみつからずにエラーになる【Kotlinサーバーサイドプログラミング実践開発】

Posted at

はじめに

以下の書籍を読んでいてKtorのライブラリについてつまづいたのでまとめます

問題

書籍P259の「Jacksonのフィーチャーを追加」のところでContetnNegotiationがみつからずコンパイルエラーとなっていました

書籍のリポジトリを確認したところ

import io.ktor.features.ContentNegotiation

こちらで入れていましたが、ktorにfeaturesというのはありませんでした

試行錯誤

まずは以下の依存を追加してみました

implementation("ktor-client-features:1.6.8")

しかし、ContentNegotiationはありませんでした

次にこちらのリポジトリを参考に以下を追加してみました

implementation("ktor-client-plugins:1.6.8")

しかしこれもうまくいきませんでした

解決方法

最終的に以下のコードにすることで解決しました

build.gradle.kt
val ktor_version: String by project
val kotlin_version: String by project
val logback_version: String by project

plugins {
    kotlin("jvm") version "1.8.0"
    id("io.ktor.plugin") version "2.2.1"
                id("org.jetbrains.kotlin.plugin.serialization") version "1.8.0"
}

group = "com.example"
version = "0.0.1"
application {
    mainClass.set("com.example.ApplicationKt")

    val isDevelopment: Boolean = project.ext.has("development")
    applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
    implementation("io.ktor:ktor-server-resources:$ktor_version")
    implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
    implementation("ch.qos.logback:logback-classic:$logback_version")
    implementation("io.ktor:ktor-server-content-negotiation:$ktor_version") // 追加
    implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version") // 追加
    implementation("io.ktor:ktor-jackson:1.6.8")
    testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}
Application.kt
package com.example

import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.routing.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.resources.*

fun main() {
    embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module)
        .start(wait = true)
}

fun Application.module() {
    install(Resources)
    install(ContentNegotiation) {
        json()
    }
    routing {
        bookRoute()
    }
}
BookRoute.kt
package com.example

import io.ktor.resources.*
import io.ktor.server.application.*
import io.ktor.server.resources.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.Serializable

fun Routing.bookRoute() {

    get<BookLocation.GetBook> { request ->
        val response = BookResponse(request.bookId, "Kotlin入門", "Kotlin太郎")
        call.respond(response)
    }
}

@Serializable
@Resource("/book")
class BookLocation(){
    @Serializable
    @Resource("/detail/{bookId}")
    class GetBook(val parent: BookLocation = BookLocation(), val bookId: Long)
}
@Serializable
data class BookResponse(
    val id: Long,
    val title: String,
    val author: String,
)

そもそもjacksonを使わなくてもktorのio.ktor:ktor-server-content-negotiationio.ktor:ktor-serialization-kotlinx-jsonを使うことで同じようなことができるようになっていました

おわりに

やはり公式ドキュメントが一番ということを何度も最近感じさせられます
Kotlinはライブラリ関係のエラーがすごく多発して大変です

参考

1
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
1
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?