在 micronaut 中发布到一个简单的 hello 控制器时出现错误。客户端似乎以 application/json 的形式发布,但控制器需要 text/plain。如何修复客户端以文本/纯文本形式发布?
HelloController.java
@Post(value = "/helloBody/{name}", consumes = MediaType.TEXT_PLAIN)
public Single<String> helloBody(@NotBlank String name, @NotBlank @Body String text) {
return Single.just(name + "sent: " + text);
}
HelloClient.java
@Post(value = "/helloBody/{name}")
Single<String> helloBody(@NotBlank String name, @NotBlank String body);
HelloControllerTest.java
@Test
void testPost() {
String body = client.helloBody("John", "message").blockingGet();
assertThat(body).isEqualTo("John sent: message");
}
错误
io.micronaut.http.client.exceptions.HttpClientResponseException: Content Type [application/json] not allowed. Allowed types: [text/plain]
at io.micronaut.http.client.DefaultHttpClient$10.channelRead0(DefaultHttpClient.java:1799)
at io.micronaut.http.client.DefaultHttpClient$10.channelRead0(DefaultHttpClient.java:1739)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
...
30秒到达战场
相关分类