You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+Kotlin启动Web应用时compileKotlin编译失败求助

解决Spring Boot + Kotlin启动时的Unresolved reference编译错误

我帮你排查了下,你遇到的Unresolved reference: web和Unresolved reference: GetMapping错误,核心原因是项目缺少Spring Web的核心依赖包,另外你的依赖版本也偏旧,可能会带来兼容性问题,下面给你具体的解决方案:

1. 补充缺失的Web Starter依赖

你的build.gradle.kts里只引入了基础的spring-boot-starter,但@GetMapping、org.springframework.web相关的类都在spring-boot-starter-web包里。需要修改dependencies块,替换或添加Web starter:

修改后的dependencies部分:

dependencies {
    // 替换原有的spring-boot-starter为web starter
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

2. 推荐升级到兼容的新版本

你当前使用的Spring Boot 2.1.9.RELEASE(2019年版本)和Kotlin 1.2.71都比较老旧,可能存在一些已知bug和兼容性问题。建议升级到稳定的兼容版本,比如:

修改plugins块的版本:

plugins {
    id("org.springframework.boot") version "2.7.15"
    id("io.spring.dependency-management") version "1.0.15.RELEASE"
    kotlin("jvm") version "1.6.21"
    kotlin("plugin.spring") version "1.6.21"
}

3. 重新构建运行

完成上述修改后,执行以下命令清理并重新构建项目:

./gradlew clean bootRun --stacktrace

为什么教程里没问题?

官方教程《Building web applications with Spring Boot and Kotlin》默认会引导开发者添加spring-boot-starter-web依赖,可能你在复制配置时遗漏了这一步,导致Web相关的注解和类无法被编译器找到。

内容的提问来源于stack exchange,提问作者shingo.nakanishi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:26:17