首页 文章

Spring Reactive Web Applications POST请求正文丢失

提问于
浏览
2

我正在研究小型测试项目,以检查Spring Reactive Web Applications如何与MongoDB实际配合使用 .

我按照https://docs.spring.io/spring/docs/5.0.0.M4/spring-framework-reference/html/web-reactive.html的说明书进行操作

它表明我可以在控制器中处理POST请求,如:

@PostMapping("/person")
    Mono<Void> create(@RequestBody Publisher<Person> personStream) {
        return this.repository.save(personStream).then();
    }

虽然这似乎不起作用 . 这是我实施的控制器:

https://github.com/pavelmorozov/reactor-poc/blob/master/src/main/java/com/springreactive/poc/controller/BanquetHallController.java

它只有一个POST映射,非常简单:

@PostMapping("/BanquetHall")
    Mono<Void> create(@RequestBody Publisher<BanquetHall> banquetHallStream) {
        return banquetHallRepository.insert(banquetHallStream).then();
    }

每次我用curl发出POST时都会调用它:

curl -v -XPOST -H "Content-type: application/json" -d '{"name":"BH22"}' 'http://localhost:8080/BanquetHall'
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /BanquetHall HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
> Content-type: application/json
> Content-Length: 15
> 
* upload completely sent off: 15 out of 15 bytes
< HTTP/1.1 200 OK
< content-length: 0
< 
* Connection #0 to host localhost left intact

我看到存储在mongodb中的新对象,但它们不包含数据 . 要调试我构建简单订阅者,以查看实际作为请求主体传递给控制器的数据:

Subscriber s = new Subscriber() {
    @Override
    public void onSubscribe(Subscription s) {
        logger.info("Argument: "+s.toString());
    }
    @Override
    public void onNext(Object t) {
        logger.info("Argument: "+t.toString());

    }
    @Override
    public void onError(Throwable t) {
        logger.info("Argument: "+t.toString());
    }
    @Override
    public void onComplete() {
        logger.info("Complete! ");
    }
};
banquetHallStream.subscribe(s);

现在我看到订阅onError方法后调用了 . Throwable状态遗体:

Request Error 400

Here error string:
Request body is missing: reactor.core.publisher.Mono<java.lang.Void> com.springreactive.poc.controller.BanquetHallController.create(org.reactivestreams.Publisher<com.springreactive.poc.domain.BanquetHall>)

Why request body is empty?

另外要知道的是:正如我对所有这些反应性内容的新手一样,在没有手动实现订阅服务器的情况下调试发布者/订阅者是否有更好的方法?

Update 我更新了POST处理程序方法描述,它将请求体传递为String对象:

Mono<Void> create(@RequestBody String banquetHallStream)

那么这不是“反应”,对吗?字符串不是被动的,因为发布者应该......

1 回答

  • 0

    我有完全相同的问题,并能够通过将 @ResponseStatus 放在方法上来解决它 . 以下是方法控制器的外观:

    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping(value = "/bulk", consumes = APPLICATION_STREAM_JSON_VALUE)
    public Mono<Void> bulkInsert(@RequestBody Flux<Quote> quotes) {
        return quoteReactiveRepository.insert(quotes).then();
    }
    

    我正在使用 WebClient 向该 endpoints 发出请求:

    webClient.post()
            .uri("/quotes/bulk")
            .contentType(MediaType.APPLICATION_STREAM_JSON)
            .body(flux(), Quote.class)
            .retrieve()
            .bodyToMono(Void.class).block();
    

    测试: org.springframework.boot:spring-boot-starter-webflux:2.1.0.RELEASE

相关问题