简体   繁体   English

Micronaut HttpClients 交换体始终为 null

[英]Micronaut HttpClients exchange body is always null

I have setup a simple test Controller:我已经设置了一个简单的测试 Controller:

@Controller("/test")
public class SampleController {
  @Get(value = "1", produces = MediaType.TEXT_PLAIN)
  public String helloWorld1() {
    return "Hello, World!";
  }

  @Get(value = "2", produces = MediaType.TEXT_PLAIN)
  public HttpResponse<String> helloWorld2() {
    return HttpResponse.ok("Hello, World!");
  }
}

And I am using the low-level HTTPClient in my Unit-Tests, which looks like this:我在我的单元测试中使用了低级 HTTPClient,它看起来像这样:

@MicronautTest
public class SampleControllerTest {

  @Inject
  EmbeddedServer server;

  @Inject
  @Client("/test")
  HttpClient client;

  @Test
  void shouldReturnHelloWorld1_1() {
    HttpResponse<String> response = client.toBlocking().exchange(HttpRequest.GET("/1").accept(
        MediaType.TEXT_PLAIN));

    assertEquals(200, response.code());
    assertEquals("Hello, World!", response.body());
  }

  @Test
  void shouldReturnHelloWorld1_2() {
    String response = client.toBlocking().retrieve(HttpRequest.GET("/1").accept(MediaType.TEXT_PLAIN));

    assertEquals("Hello, World!", response);
  }

  @Test
  void shouldReturnHelloWorld2() {
    HttpResponse<String> response = client.toBlocking().exchange(HttpRequest.GET("/2").accept(
        MediaType.TEXT_PLAIN));

    assertEquals(200, response.code());
    assertEquals("Hello, World!", response.body());
  }
}

From my understanding the response body should never be null, however it is for the tests shouldReturnHelloWorld2 and shouldReturnHelloWorld1_1 - so it is always null when HttpClient.exchange() is used.据我了解,响应正文不应该是 null,但它是用于测试shouldReturnHelloWorld2shouldReturnHelloWorld1_1 - 所以当使用HttpClient.exchange()时它总是 null。 In my opinion this seems to be bug or is here any issue?在我看来,这似乎是错误还是这里有什么问题?

You can check the whole code and run the tests yourself by cloning my sample repository: https://github.com/tobi6112/micronaut-httpclient-issue您可以通过克隆我的示例存储库来检查整个代码并自己运行测试: https://github.com/tobi6112/micronaut-httpclient-issue

Update: Just noticed that the tests work as expected with更新:刚刚注意到测试按预期工作

HttpResponse<String> response = client.toBlocking()
        .exchange(HttpRequest.GET("/2").accept(MediaType.TEXT_PLAIN), String.class);

In my case these two options work:就我而言,这两个选项有效:

final var result = client.toBlocking().exchange(HttpRequest.GET(url).accept(MediaType.APPLICATION_JSON), String.class);

HttpResponse<String> response  = client.toBlocking().exchange(HttpRequest.GET(url).accept(MediaType.APPLICATION_JSON), String.class);

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

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