【问题标题】:How to "Unresolved reference error: DefaultHeaders" in kotlin如何在kotlin中“未解决的参考错误:DefaultHeaders”
【发布时间】:2020-02-03 07:35:38
【问题描述】:

这是我基于 ktor.io (https://ktor.io/quickstart/quickstart/gradle.html) 上的快速启动公会的代码

package blog

import io.ktor.application.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*

import org.jetbrains.ktor.features.CallLogging


fun Application.main() {
    install(DefaultHeaders)
    install(CallLogging)
    install(Routing) {
        get("/") {
            call.respondText("My Example Blog2", ContentType.Text.Html)
        }
    }
}

当我运行gradle build 时,我得到了这些错误:

e: /Users/antkong/dev/poc/kotlin/src/main/kotlin/BlogApp.kt: (10, 22): 未解决的参考:ktor

e: /Users/antkong/dev/poc/kotlin/src/main/kotlin/BlogApp.kt: (14, 13): 未解决的参考:DefaultHeaders e:

e: /Users/antkong/dev/poc/kotlin/src/main/kotlin/BlogApp.kt: (15, 13): 未解决的参考:CallLogging

这是我的 build.gradle 文件

group 'Example'
buildscript {
    ext.kotlin_version = '1.3.61'
    ext.ktor_version = '1.3.0'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'idea'
apply plugin: 'application'

mainClassName = 'io.ktor.server.netty.EngineMain' 

sourceCompatibility = 1.8
compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

kotlin {
}

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    compile "io.ktor:ktor-server-netty:$ktor_version"
    compile "ch.qos.logback:logback-classic:1.2.3"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

如何修复这些错误?

【问题讨论】:

    标签: gradle kotlin


    【解决方案1】:

    这些工件在另一个包中,您的导入应如下所示:

    import io.ktor.application.*
    import io.ktor.features.CallLogging
    import io.ktor.features.DefaultHeaders
    import io.ktor.http.*
    import io.ktor.response.*
    import io.ktor.routing.*
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-08
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多