想法 Kotlin Spring webflux +协程调试。评估错误“this@未被捕获”

发布于 2025-01-17 03:12:25 字数 2108 浏览 1 评论 0原文

尝试使用 spring weblux 和 kotlin 协程创建简单的应用程序。 像这样

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}

@RestController
class TestController(
    private val testService: TestService
){
    @GetMapping
    suspend fun test() {
        testService.test()
    }
}

@Service
class TestService() {
    private val testMap: Map<Int, String> = mapOf(1 to "1")

    suspend fun test() {
        println("service")
    }
}

如果在服务 fun 上设置断点并尝试使用评估来调用 testMap 返回错误“this@TestService 未捕获” 输入图片此处的描述

我怎样才能正确地对这样的应用程序使用调试?

UPD1

项目 build.gradle.kts 示例代码:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.6.5"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.6.10"
    kotlin("plugin.spring") version "1.6.10"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("io.projectreactor:reactor-test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

测试:

  • Java:11-17
  • Kotlin 插件:213-1.6.10-release-961-IJ6777.52

Try to create simple application with spring weblux and kotlin coroutines.
Like this

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}

@RestController
class TestController(
    private val testService: TestService
){
    @GetMapping
    suspend fun test() {
        testService.test()
    }
}

@Service
class TestService() {
    private val testMap: Map<Int, String> = mapOf(1 to "1")

    suspend fun test() {
        println("service")
    }
}

If set breakpoint on service fun and try to use evaluate for invoke testMap return error "this@TestService is not captured"
enter image description here

How can I correctly use debug for applications like this?

UPD1

Project build.gradle.kts example code:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.6.5"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.6.10"
    kotlin("plugin.spring") version "1.6.10"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("io.projectreactor:reactor-test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

Testing on:

  • Java: 11-17
  • Kotlin plugin: 213-1.6.10-release-961-IJ6777.52

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文