【问题标题】:Does Springboot webflux support send Flux<Object> request from client to server?Spring Boot webflux 是否支持将 Flux<Object> 请求从客户端发送到服务器?
【发布时间】:2018-11-10 08:27:54
【问题描述】:

Springboot webflux 是否支持从客户端向服务器发送 Flux 请求?

从我的测试中,我可以得到来自服务器的 Flux 响应,但是如何向服务器发送 Flux 请求呢?

服务器端:

 @PostMapping(value = "/stream/numbers2", produces = MediaType.APPLICATION_STREAM_JSON_VALUE,
      consumes = MediaType.APPLICATION_STREAM_JSON_VALUE)
  public Flux<StreamNumber> streamNumbers2(@RequestBody Publisher<Integer> request)
      throws InterruptedException {
    log.info("streamNumbers API ");

    return Flux.from(request).map(item -> {
      try {
        Thread.sleep(1000);
        StreamNumber sn = new StreamNumber();
        sn.setOriginNumber(item);
        sn.setNewNumber(item + 1);
        sn.setOperation("Plus 1");
        return sn;
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      return new StreamNumber(0, 0, "Error");
    }).doOnNext(item -> log.info("Number: {}", item));
  }

客户端:

  public void testPost() throws Exception {
    log.info("Test webflux post flux");
    WebClient webClient = WebClient.create("http://localhost:8080/webflux/stream/numbers2");
    Flux<Integer> request = Flux.just(1, 2, 3, 4, 5, 6, 7, 8);

    webClient.post().contentType(MediaType.APPLICATION_STREAM_JSON)
        .body(request, Integer.class)
        .accept(MediaType.APPLICATION_STREAM_JSON)
        .retrieve()
        .bodyToFlux(StreamNumber.class)
        .map(item -> {
          log.info("result: {}", item.toString());
          return item;
        }).collectList().block();
  }

从服务器端获取错误信息,

[21:25:28.555] [ERROR] [http-nio-8080-exec-5] - Forwarding to error page from request [/webflux/stream/numbers2] due to exception [Type definition error: [simple type, class org.reactivestreams.Publisher]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.reactivestreams.Publisher` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (PushbackInputStream); line: 1, column: 1]]
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.reactivestreams.Publisher]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.reactivestreams.Publisher` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (PushbackInputStream); line: 1, column: 1]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:238) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:223) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:206) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:157) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:130) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:131) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]

【问题讨论】:

  • 一个请求就是一个请求,你必须发布,服务器会相应地处理。
  • @stephane-nicoll
  • @AshishRatan 所以 webflux 是单向流式处理
  • @PostMapping("/person") Mono create(@RequestBody Publisher personStream) { return this.repository.save(personStream).then(); }
  • 我不明白这个问题。你能解释一下你的方法发生了什么吗?发生了什么(你能提供一个堆栈跟踪)吗?你期望它应该如何表现?它的行为如何?

标签: spring spring-boot streaming flux spring-webflux


【解决方案1】:

堆栈跟踪表明您使用的是 Spring MVC,而不是 Spring WebFlux。 请检查您的依赖项并确保您没有将 Spring MVC 作为传递依赖项。

如果 Spring Boot 在类路径中同时选择了 MVC 和 WebFlux,则 MVC 将接管。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 2014-09-24
    相关资源
    最近更新 更多