简体   繁体   English

Spring Reactive Web Applications POST请求正文丢失

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

I work on small test project to check how Spring Reactive Web Applications actually works with MongoDB. 我从事小型测试项目,以检查Spring Reactive Web Applications如何与MongoDB实际配合使用。

I follow the manual from https://docs.spring.io/spring/docs/5.0.0.M4/spring-framework-reference/html/web-reactive.html 我按照手册来自https://docs.spring.io/spring/docs/5.0.0.M4/spring-framework-reference/html/web-reactive.html

and it states that I can process POST request in controller like: 它表明我可以在控制器中处理POST请求,如:

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

Though this seems not works. 虽然这似乎不起作用。 Here the controller I implemented: 这是我实施的控制器:

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

it have just one POST mapping and it is very simple: 它只有一个POST映射,非常简单:

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

It is called each time I issue a POST with curl: 每次我用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

And I see new objects stored in mongodb, but they not contain data. 我看到存储在mongodb中的新对象,但它们不包含数据。 To debug I build simple subscriber, to see the data actually passed as request body to controller: 要调试我构建简单订阅者,以查看实际作为请求主体传递给控制器​​的数据:

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);

and now I see after subscription onError method called. 现在我看到订阅onError方法后调用了。 The Throwable states body missing: 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>)

Why request body is empty? 为什么要求身体是空的?

Also good to know: As I new with all this reactive stuff, could it be some better approach to debug Publisher/Subscriber without manual implementing Subscriber? 另外要知道的是:当我对所有这些被动的东西感到陌生时,如果没有手动实现Subscriber,调试Publisher / Subscriber会有更好的方法吗?

Update I updated POST handler method description and it passes request body as String object: 更新我更新的POST处理程序方法描述,它将请求主体作为String对象传递:

Mono<Void> create(@RequestBody String banquetHallStream)

Then this is not a "Reactive", right? 那么这不是“反应性”,对吗? String is not reactive, as Publisher should be... 字符串不是被动的,因为发布者应该......

I had exact the same issue and was able to solve it by putting @ResponseStatus on method. 我有完全相同的问题,并能够通过将@ResponseStatus放在方法@ResponseStatus解决它。 Below is how method controller looks like: 以下是方法控制器的外观:

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

I'm doing the request to that endpoint using WebClient : 我正在使用WebClient向该端点发出请求:

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

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

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

相关问题 缺少请求正文的 Spring Boot POST 请求? - Spring Boot POST request with missing request body? spring 模拟 mvc 执行发布请求正文丢失:错误 400 - spring mock mvc perform post request body is missing : error 400 Spring boot post rest调用“缺少所需的请求正文” - Spring boot post rest call "Required request body is missing" Spring 反应式应用程序上的请求/响应正文的副本? - Copy of the request/response body on a Spring reactive app? 订阅请求正文未在 Spring Reactive 中发送值 - Subscription to request body not emmitting values in Spring Reactive 如何使用WebClient反应式Web客户端发送带有zip正文的POST请求 - How can I send POST request with zip body using WebClient reactive web client 如何从 Spring WebFlux 反应式中的 ServerRequest 对象获取请求正文? - How to get request body from ServerRequest object in Spring WebFlux reactive? 将 CSV 文件写入 Spring Reactive WebClient 的请求正文 - Writing CSV file to a request body of Spring Reactive WebClient Spring 框架 HttpMessageNotReadableException:缺少所需的请求正文 - Spring framework HttpMessageNotReadableException: Required request body is missing 缺少所需的请求正文:在 spring 启动和 angular 9 - Required request body is missing:in spring boot and angular 9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM