【发布时间】:2018-06-06 07:16:49
【问题描述】:
该文档说:https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html
ServerResponse 提供对 HTTP 响应的访问。由于它是不可变的,因此您可以使用构建器创建一个 ServerResponse。构建器允许您设置响应状态、添加响应标头并提供正文。例如,这是如何创建具有 200 OK 状态、JSON 内容类型和正文的响应:
Mono<Person> person = ...
ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(person);
同样,我厌倦了将Mono<T> 传递给 ServerReponse Builder 的 body 方法,但出现以下错误:
代码sn-p:
编译错误:
Mono<Inventory> inventoryMono=request.bodyToMono(Inventory.class);
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(inventoryMono);
但它适用于以下代码:
ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(inventoryMono.doOnNext(inventoryRepository::save).log(),Inventory.class)
我错过了什么?
【问题讨论】:
-
你试过了吗,ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(inventoryMono,inventory.class);
标签: spring spring-boot spring-webflux