Spring Boot(Kotlin)中RSocket请求响应模式Header处理异常问题
RSocket自定义Header无法获取问题
我在测试RSocket请求响应逻辑时,遇到了自定义Header无法获取的问题。技术栈为Spring Boot 2.7.9 + Kotlin,Controller通过@Headers注解获取Header,但使用rsocket-cli发送携带自定义Header(foo:bar、hello:world)的请求后,控制台仅输出框架内置Header,无法获取到自定义Header,查阅官方文档后仍未解决。
以下是项目相关代码、配置及测试信息:
Controller代码
package com.mol.rsocket.controller import com.mol.rsocket.model.HelloRequest import org.springframework.messaging.handler.annotation.Headers import org.springframework.messaging.handler.annotation.MessageMapping import org.springframework.stereotype.Controller import reactor.core.publisher.Mono import reactor.kotlin.core.publisher.toMono @Controller class Controller { @MessageMapping("hello") fun currentMarketData( request: HelloRequest, @Headers headers: Map<String, String>, ): Mono<String> { println("Headers ${headers.keys}") return "Hello ${request.name}!!".toMono() } }
build.gradle配置
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { id("org.springframework.boot") version "2.7.9" id("io.spring.dependency-management") version "1.0.15.RELEASE" kotlin("jvm") version "1.6.21" kotlin("plugin.spring") version "1.6.21" } group = "com.mol" version = "0.0.1-SNAPSHOT" java.sourceCompatibility = JavaVersion.VERSION_17 repositories { mavenCentral() } dependencies { implementation("org.springframework.boot:spring-boot-starter-rsocket") 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.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 = "17" } } tasks.withType<Test> { useJUnitPlatform() }
测试命令
rsocket-cli --route=hello -i "{\"name\": \"Kirill\"}" --header foo:bar --header hello:world --request tcp://localhost:7000
控制台输出
Headers [dataBufferFactory, rsocketRequester, rsocketResponse, lookupDestination, contentType, rsocketFrameType]
内容的提问来源于stack exchange,提问作者Кирилл Мололкин
相关产品推荐
相关产品推荐

