jackson ktor Apache客户端发布请求-未找到转换:字节缓冲区通道

1l5u6lss  于 2022-11-08  发布在  Apache
关注(0)|答案(1)|浏览(124)

我必须使用非常旧的ktor版本(1.2.6)-请不要告诉我升级,我现在不能这样做。我试图发送一个后请求到另一个服务,我嘲笑测试与wiremock。
这是我的客户端配置:

object HTTP {
    val client = HttpClient(Apache) {
        followRedirects = false
        engine {
            customizeClient {
                setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
            }
        }

        install(JsonFeature) {
            serializer = JacksonSerializer()
        }
    }
}

这段代码实际上是尝试执行post:

val url = "http://localhost:9099/v0/test"
val response = HTTP.client.post<TestResponse>(url) {
        header(HttpHeaders.ContentType, "application/json")
        contentType(ContentType.Application.Json)
        body = TestRequest()
}

这是我的无线电配置:

WireMock.stubFor(
  WireMock.post(WireMock.urlMatching("/v0/test"))
     .willReturn(
        WireMock.aResponse().withStatus(200)
           .withBody(jacksonObjectMapper().writeValueAsString(testResponse))
     )
)

当我用curl点击这个wiremock时,它工作正常,但是用上面的代码调用它会导致:

No transformation found: class kotlinx.coroutines.io.ByteBufferChannel -> class com.test.TestResponse
io.ktor.client.call.NoTransformationFoundException: No transformation found: class kotlinx.coroutines.io.ByteBufferChannel -> class com.test.TestResponse
    at io.ktor.client.call.HttpClientCall.receive(HttpClientCall.kt:88)

有人能帮帮忙吗?

bkkx9g8r

bkkx9g8r1#

要解决您的问题,请将Content-Type: application/json标头添加到响应中:

stubFor(
    post(urlMatching("/v0/test"))
        .willReturn(
            aResponse().withStatus(200)
                .withHeader("Content-Type", "application/json")
                .withBody(jacksonObjectMapper().writeValueAsString(TestResponse(123)))
        )
)

相关问题