繁体   English   中英

Spring Reactive Web Applications POST请求正文丢失

[英]Spring Reactive Web Applications POST Request body is missing

我从事小型测试项目,以检查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状态遗体:

请求错误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>)

为什么要求身体是空的?

另外要知道的是:当我对所有这些被动的东西感到陌生时,如果没有手动实现Subscriber,调试Publisher / Subscriber会有更好的方法吗?

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

Mono<Void> create(@RequestBody String banquetHallStream)

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

我有完全相同的问题,并能够通过将@ResponseStatus放在方法@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向该端点发出请求:

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

暂无
暂无

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

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