[英]How to view response from Spring 5 Reactive API in Postman?
I have next endpoint in my application: 我的应用程序中有下一个端点:
@GetMapping(value = "/users")
public Mono<ServerResponse> users() {
Flux<User> flux = Flux.just(new User("id"));
return ServerResponse.ok()
.contentType(APPLICATION_JSON)
.body(flux, User.class)
.onErrorResume(CustomException.class, e -> ServerResponse.notFound().build());
}
Currently I can see text "data:"
as a body and Content-Type →text/event-stream
in Postman. 目前我可以在Postman中看到文本
"data:"
作为正文和Content-Type →text/event-stream
。 As I understand Mono<ServerResponse>
always return data with SSE(Server Sent Event)
. 据我所知,
Mono<ServerResponse>
始终使用SSE(Server Sent Event)
返回数据。 Is it possible to somehow view response in Postman client? 有可能以某种方式查看Postman客户端的响应吗?
It seems you're mixing the annotation model and the functional model in WebFlux. 看来你正在混合注释模型和WebFlux中的功能模型。 The
ServerResponse
class is part of the functional model. ServerResponse
类是功能模型的一部分。
Here's how to write an annotated endpoint in WebFlux: 以下是如何在WebFlux中编写带注释的端点:
@RestController
public class HomeController {
@GetMapping("/test")
public ResponseEntity serverResponseMono() {
return ResponseEntity
.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(Flux.just("test"));
}
}
Here's the functional way now: 这是现在的功能方式:
@Component
public class UserHandler {
public Mono<ServerResponse> findUser(ServerRequest request) {
Flux<User> flux = Flux.just(new User("id"));
return ServerResponse.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(flux, User.class)
.onErrorResume(CustomException.class, e -> ServerResponse.notFound().build());
}
}
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public RouterFunction<ServerResponse> users(UserHandler userHandler) {
return route(GET("/test")
.and(accept(MediaType.APPLICATION_JSON)), userHandler::findUser);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.