Controller with suspend function causing netty.RoutingInBoundHandler - java.lang.NoClassDefFoundError: kotlinx/coroutines/ThreadContextElement

Controller with suspend function causing netty.RoutingInBoundHandler - java.lang.NoClassDefFoundError: kotlinx/coroutines/ThreadContextElement

我正在尝试创建最简单的控制器挂起函数,它将调用另一个端点。学习目的是了解如何在Kotlin中使用Suspend函数和Coroutine。

为此,我创建了一个挂起函数作为端点,它从服务层调用另一个挂起函数,该服务层将使用 JDK HttpClient 调用另一个端点。

这是控制器

package com.tolearn.controller

import com.tolearn.service.DemoService
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import java.net.http.HttpResponse
import javax.inject.Inject

@Controller("/tolearn")
class DemoController {

    @Inject
    lateinit var demoService: DemoService

    @Get
    @Produces(MediaType.TEXT_PLAIN)
    suspend fun getOtherEndpointViaCoroutine(): HttpResponse<String> {

        return demoService.fetchUrl()
    }
}

服务

package com.tolearn.service

import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.time.Duration
import javax.inject.Singleton

@Singleton
class DemoService {

    suspend fun fetchUrl(): HttpResponse<String> {

        val client: HttpClient = HttpClient.newBuilder()
                .version(HttpClient.Version.HTTP_2)
                .followRedirects(HttpClient.Redirect.NEVER)
                .connectTimeout(Duration.ofSeconds(20))
                .build()

        val request = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:3000/employees"))
                .build()

        val response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());

        return response.get()
    }

}

build.gradle

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.4.10"
    id("org.jetbrains.kotlin.kapt") version "1.4.10"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.4.10"
    id("com.github.johnrengelman.shadow") version "6.1.0"
    id("io.micronaut.application") version "1.2.0"
}

version = "0.1"
group = "com.tolearn"

repositories {
    mavenCentral()
    jcenter()
}

micronaut {
    runtime("netty")
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("com.tolearn.*")
    }
}

dependencies {
    implementation("io.micronaut:micronaut-validation")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut:micronaut-runtime")
    implementation("javax.annotation:javax.annotation-api")
    implementation("io.micronaut:micronaut-http-client")

    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.4.2")

    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
}


application {
    mainClass.set("com.tolearn.ApplicationKt")
}

java {
    sourceCompatibility = JavaVersion.toVersion("11")
}

tasks {
    compileKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }

}

我用

调用端点
curl --location --request GET 'localhost:8080/tolearn'

我收到这个错误

C:\Users\Public\Java\jdk-11.0.9+11\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:54690,suspend=y,server=n -javaagent:C:\Users\DEMETRC\AppData\Local\JetBrains\IdeaIC2020.2\groovyHotSwap\gragent.jar -javaagent:C:\Users\DEMETRC\AppData\Local\JetBrains\IdeaIC2020.2\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "C:\Users\DEMETRC\_c\to_learn\demo-coroutine\build\classes\kotlin\main;C:\Users\DEMETRC\_c\to_learn\demo-coroutine\build\tmp\kapt3\classes\main;C:\Users\DEMETRC\_c\to_learn\demo-coroutine\build\resources\main;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-validation.2.1b346cacc3e3c3ae08952e34add3fb335b99d5c9\micronaut-validation-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jdk8.4.108caa30623f73223194a8b657abd2baec4880ea\kotlin-stdlib-jdk8-1.4.10.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-reflect.4.10\e2b3c6695eee6085e606d96d685396dce23a3a06\kotlin-reflect-1.4.10.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut.kotlin\micronaut-kotlin-runtime.2.0\b8b24a7e5ea4d68afd6edc283f2b1f944bbf2cc7\micronaut-kotlin-runtime-2.2.0.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-http-client.2.1\f58e0b275df5a4c2fda59083a631a10efc51b038\micronaut-http-client-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-http-server-netty.2.1dc929dca3e3068638e6a22544a1fbd023fa06b\micronaut-http-server-netty-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-runtime.2.1b934a4fa0a2ccb8b98be40a05cdb297e5516b2\micronaut-runtime-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-inject.2.1\fa4267534151e925acd48339113555ab15222ae1\micronaut-inject-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\javax.annotation\javax.annotation-api.3.24c04d3cfef185a8008e7bf34331b79730a9d43\javax.annotation-api-1.3.2.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-http.2.1fbf76299ba6d433f7bb18b11e5a1f7613c286e\micronaut-http-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api.7.26100a62c2e6f04b53977b9f541044d7d722693d\slf4j-api-1.7.26.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\javax.validation\validation-api.0.1.Final\cb855558e6271b1b32e716d24cb85c7f583ce09e\validation-api-2.0.1.Final.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jdk7.4.10e46450b0bb3dbf43898d2f461be4a942784780\kotlin-stdlib-jdk7-1.4.10.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib.4.10\ea29e063d2bbe695be13e9d044dcfb0c7add398e\kotlin-stdlib-1.4.10.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-http-client-core.2.1\f1153f5acb025c9f2330670054c07c94f75aa89c\micronaut-http-client-core-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-websocket.2.1\a0db7bc65c3c504f1908a35191cd4bc3eee3a597\micronaut-websocket-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.netty\netty-handler-proxy.1.54.Finalfa9d18b09a8a79bf64e8bc1bd775cbd77277199\netty-handler-proxy-4.1.54.Final.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-http-server.2.1d341f2ee609d14acd481049de9206b0607683d\micronaut-http-server-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-http-netty.2.15a31775ec7c170282c5ad34177edd07247508b\micronaut-http-netty-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.netty\netty-codec-http.1.54.Finaleb9509289d1cee549cf12bae71929d1a4a12c7\netty-codec-http-4.1.54.Final.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-aop.2.1\a8e591272fc3b5e0943f4ccc9ae53206445b5f46\micronaut-aop-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.reactivex.rxjava2\rxjava.2.10e9edc67e0abaa03713eeb9ca2cb0e30c859de4\rxjava-2.2.10.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.datatype\jackson-datatype-jdk8.11.2\d4c1933a8d62db65c3d5a5cd809511e021a189c0\jackson-datatype-jdk8-2.11.2.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.datatype\jackson-datatype-jsr310.11.2\e6235e5eb3cf3edd2a95cd0dc96bc48aeb309e8a\jackson-datatype-jsr310-2.11.2.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-core.2.1\c4e4f05efc2e97dbf7a8f4fe02ab89ad0acfc13\micronaut-core-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\org.yaml\snakeyaml.26\a78a8747147d2c5807683e76ec2b633e95c14fe9\snakeyaml-1.26.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\javax.inject\javax.inject75da39a7040257bd51d21a231b76c915872d38\javax.inject-1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-common.4.1029be3465805c99db1142ad75e6c6ddeac0b04c\kotlin-stdlib-common-1.4.10.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\org.jetbrains\annotations.09f0dfe192fb4e063e7dacadee7f8bb9a2672a9\annotations-13.0.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.netty\netty-codec-socks.1.54.Finalf8239e989e80cd470eaea90ee5746dbf24f5b8e\netty-codec-socks-4.1.54.Final.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.netty\netty-codec.1.54.Final5680345b12a86c21fa7af5d9a77e0e0fcff46a\netty-codec-4.1.54.Final.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.netty\netty-transport.1.54.Final\ddb9a255819b87b8812932d1d1001f4d9dc7f89e\netty-transport-4.1.54.Final.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.netty\netty-buffer.1.54.Finalc513ef8a2e56e88d3736752ea10eae758f47d8\netty-buffer-4.1.54.Final.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.netty\netty-common.1.54.Finalc330383cda87204ab38e7401ab0f56c0d43b799\netty-common-4.1.54.Final.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-router.2.1\f5f0128e8741af1a88358496a72a2170fe112f73\micronaut-router-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.micronaut\micronaut-buffer-netty.2.13578d211959e86863a451d698292cc626cd0d3\micronaut-buffer-netty-2.2.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.netty\netty-codec-http2.1.54.Final7a4ae62b0a41cdf02f610f06eb353f10c17ef8\netty-codec-http2-4.1.54.Final.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.netty\netty-handler.1.54.Final\e83dfe8ebe3622d2cd5cce8532a9ca49cad51e9\netty-handler-4.1.54.Final.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\org.reactivestreams\reactive-streams.0.3\d9fb7a7926ffa635b3dcaa5049fb2bfa25b3e7d0\reactive-streams-1.0.3.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\com.github.spotbugs\spotbugs-annotations.0.35cc5d84d32b31beeaf8597181f0fc4eac98e16\spotbugs-annotations-4.0.3.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\io.netty\netty-resolver.1.54.Finaleed781e7531b990f8360e1a5010b53ca73878f5\netty-resolver-4.1.54.Final.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\com.google.code.findbugs\jsr305.0.2ea2e8b0c338a877313bd4672d3fe056ea78f0d\jsr305-3.0.2.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.module\jackson-module-kotlin.11.3\ad8d29545c5ab0cdd6d49ee38f7ece8d9f772815\jackson-module-kotlin-2.11.3.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-classic.2.3c4f3c474fb2c041d8028740440937705ebb473a\logback-classic-1.2.3.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\com.typesafe\config.4.1058a07624a87f90d129af7cd9c68bee94535a9\config-1.4.1.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind.11.3f7b27416934dc929bb6c2d2c5fe521829e6a4ec\jackson-databind-2.11.3.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations.11.3d4e9c777e7a8805c4a000a8629d3009c779c9b\jackson-annotations-2.11.3.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-core.2.34344400c3d4d92dfeb0a305dc87d953677c03c\logback-core-1.2.3.jar;C:\Users\DEMETRC\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core.11.3\c2351800432bdbdd8284c3f5a7f0782a352aa84a\jackson-core-2.11.3.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2020.2.2\lib\idea_rt.jar" com.tolearn.ApplicationKt
Connected to the target VM, address: '127.0.0.1:54690', transport: 'socket'
19:25:39.423 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 5074ms. Server Running: http://localhost:8080
19:26:14.218 [default-nioEventLoopGroup-1-3] ERROR i.m.h.s.netty.RoutingInBoundHandler - Unexpected error occurred: kotlinx/coroutines/ThreadContextElement
java.lang.NoClassDefFoundError: kotlinx/coroutines/ThreadContextElement
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at io.micronaut.http.bind.binders.CustomContinuation.<init>(ContinuationArgumentBinder.kt:59)
    at io.micronaut.http.bind.binders.ContinuationArgumentBinder.bind(ContinuationArgumentBinder.kt:36)
    at io.micronaut.http.bind.binders.ContinuationArgumentBinder.bind(ContinuationArgumentBinder.kt:31)
    at io.micronaut.http.server.binding.RequestArgumentSatisfier.getValueForArgument(RequestArgumentSatisfier.java:129)
    at io.micronaut.http.server.netty.NettyRequestArgumentSatisfier.getValueForArgument(NettyRequestArgumentSatisfier.java:57)
    at io.micronaut.http.server.binding.RequestArgumentSatisfier.fulfillArgumentRequirements(RequestArgumentSatisfier.java:86)
    at io.micronaut.http.server.netty.RoutingInBoundHandler.handleRouteMatch(RoutingInBoundHandler.java:675)
    at io.micronaut.http.server.netty.RoutingInBoundHandler.channelRead0(RoutingInBoundHandler.java:553)
    at io.micronaut.http.server.netty.RoutingInBoundHandler.channelRead0(RoutingInBoundHandler.java:148)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:102)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.micronaut.http.netty.stream.HttpStreamsHandler.channelRead(HttpStreamsHandler.java:199)
    at io.micronaut.http.netty.stream.HttpStreamsServerHandler.channelRead(HttpStreamsServerHandler.java:121)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:93)
    at io.netty.handler.codec.http.HttpServerKeepAliveHandler.channelRead(HttpServerKeepAliveHandler.java:64)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.flow.FlowControlHandler.dequeue(FlowControlHandler.java:200)
    at io.netty.handler.flow.FlowControlHandler.channelRead(FlowControlHandler.java:162)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:296)
    at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
    at io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ClassNotFoundException: kotlinx.coroutines.ThreadContextElement
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 78 common frames omitted
19:26:33.630 [default-nioEventLoopGroup-1-5] ERROR i.m.h.s.netty.RoutingInBoundHandler - Unexpected error occurred: kotlinx/coroutines/ThreadContextElement
java.lang.NoClassDefFoundError: kotlinx/coroutines/ThreadContextElement
    at io.micronaut.http.bind.binders.CustomContinuation.<init>(ContinuationArgumentBinder.kt:59)
    at io.micronaut.http.bind.binders.ContinuationArgumentBinder.bind(ContinuationArgumentBinder.kt:36)
    at io.micronaut.http.bind.binders.ContinuationArgumentBinder.bind(ContinuationArgumentBinder.kt:31)
    at io.micronaut.http.server.binding.RequestArgumentSatisfier.getValueForArgument(RequestArgumentSatisfier.java:129)
    at io.micronaut.http.server.netty.NettyRequestArgumentSatisfier.getValueForArgument(NettyRequestArgumentSatisfier.java:57)
    at io.micronaut.http.server.binding.RequestArgumentSatisfier.fulfillArgumentRequirements(RequestArgumentSatisfier.java:86)
    at io.micronaut.http.server.netty.RoutingInBoundHandler.handleRouteMatch(RoutingInBoundHandler.java:675)
    at io.micronaut.http.server.netty.RoutingInBoundHandler.channelRead0(RoutingInBoundHandler.java:553)
    at io.micronaut.http.server.netty.RoutingInBoundHandler.channelRead0(RoutingInBoundHandler.java:148)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:102)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.micronaut.http.netty.stream.HttpStreamsHandler.channelRead(HttpStreamsHandler.java:199)
    at io.micronaut.http.netty.stream.HttpStreamsServerHandler.channelRead(HttpStreamsServerHandler.java:121)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:93)
    at io.netty.handler.codec.http.HttpServerKeepAliveHandler.channelRead(HttpServerKeepAliveHandler.java:64)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.flow.FlowControlHandler.dequeue(FlowControlHandler.java:200)
    at io.netty.handler.flow.FlowControlHandler.channelRead(FlowControlHandler.java:162)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:296)
    at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
    at io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:834)

我把悬浮词去掉然后它就可以正常工作了。我是认真的 returns 内容。

所以我的直截了当的问题是:是否可以创建一个带有 suspending 关键字的控制器来作为协同程序工作?控制器作为挂起功能听起来很奇怪吗?

从代码片段中,可以推断该应用程序 运行 使用 IntelliJ。

这个字符串: C:\Users\Public\Java\jdk-11.0.9+11\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:54690,suspend=y,server=n -javaagent:C:\Users\DEMETRC\AppData\Local\JetBrains\IdeaIC2020.2\groovyHotSwap\gragent.jar -javaagent:C:\Users\DEMETRC\AppData\Local\JetBrains\IdeaIC2020.2\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "... 是 IntelliJ 的实际命令 运行。

依赖项 kotlinx-coroutines-core 未在 classpath 中列出,因此无法找到 class ThreadContextElement。此依赖项已列出 build.gradle,但出于某种原因 IntelliJ 无法识别它。


这种不匹配可能是由于对 build.gradle 的编辑后没有跟 reload。所以 IntelliJ 坚持使用旧的依赖项列表。

如果刷新不能解决问题,可以尝试直接 运行 将主 class 与 gradle 连接,即 ./gradlew run on Mac和 Linux.