简体   繁体   English

如何在micronaut中用生成的客户端发布

[英]How to post with generated client in micronaut

I'm getting an error when posting to a simple hello controller in micronaut. 发布到micronaut中的简单hello控制器时出现错误。 The client seems to be posting as application/json but the controller expects text/plain. 客户端似乎以application / json的形式发布,但控制器期望文本/纯文本。 How do I fix the client to post as text/plain? 如何修复客户端以文本/纯文本形式发布?

HelloController.java 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 HelloClient.java

@Post(value = "/helloBody/{name}")
Single<String> helloBody(@NotBlank String name, @NotBlank String body);

HelloControllerTest.java HelloControllerTest.java

@Test
void testPost() {
    String body = client.helloBody("John", "message").blockingGet();
    assertThat(body).isEqualTo("John sent: message");
}

Error 错误

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)
...

You can change this by tell the client to send text/plain as the backend expects. 您可以通过告诉客户端按照后端的要求发送text/plain来更改此设置。

@Post(value = "/helloBody/{name}", consumes = MediaType.TEXT_PLAIN, produces = MediaType.TEXT_PLAIN)
Single<String> helloBody(@NotBlank String name, @NotBlank String body);

The idea in Micronaut is to make web operations shareable between a controller and a client. Micronaut中的想法是使Web操作在控制器和客户端之间可共享。

Let's think of a interface called HelloOperations . 让我们考虑一个名为HelloOperations的接口。 This is your web interface that can be shared between the client and server. 这是可以在客户端和服务器之间共享的Web界面。

@Validated
public interface HelloOperations {

  @Post(value = "/helloBody/{name}", consumes = MediaType.TEXT_PLAIN)
  Single<String> helloBody(@NotBlank String name, @NotBlank @Body String text);
}

Now you start implementing the contract in the backend with a controller implementation such as 现在,您开始使用诸如以下的控制器实现在后端实现合同

@Controller
public class HelloController implements HelloOperations {

   @Override
   public Single<String> helloBody(String name, String text)  {

     // do something in here...
   }
}

after that you continue on the client side. 之后,您将继续在客户端。

@Client
public interface HelloWorldClient extends HelloOperations {

   @Override
   Single<String> helloBody(String name, String text);
}

Et voilà. 等等。 You successfully connected them. 您已成功连接它们。

Further explanations can be found in the Micronaut documentation at https://docs.micronaut.io/latest/guide/index.html#clientAnnotation 进一步的说明可以在Micronaut文档中找到, 网址https://docs.micronaut.io/latest/guide/index.html#clientAnnotation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM