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]
答案 0 :(得分:0)
堆栈跟踪表明您使用的是Spring MVC,而不是Spring WebFlux。 请检查您的依赖项,并确保您没有将Spring MVC作为传递依赖项。
如果Spring Boot在类路径中选择了MVC和WebFlux,MVC将接管。