如何在Postman中查看Spring 5 Reactive API的响应?

编程入门 行业动态 更新时间:2024-10-23 16:16:12
本文介绍了如何在Postman中查看Spring 5 Reactive API的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的应用程序中有下一个端点:

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

当前,我可以看到文本 data: 作为正文,在邮递员中使用 Content-Type→文本/事件流。据我了解, Mono< ServerResponse> 始终使用 SSE(服务器发送事件)返回数据。 是否可以在邮递员客户端中以某种方式查看响应?

Currently I can see text "data:" as a body and Content-Type →text/event-stream in Postman. As I understand Mono<ServerResponse> always return data with SSE(Server Sent Event). Is it possible to somehow view response in Postman client?

推荐答案

似乎您正在混合注释模型和WebFlux中的功能模型。 ServerResponse 类是功能模型的一部分。

It seems you're mixing the annotation model and the functional model in WebFlux. The ServerResponse class is part of the functional model.

以下是在WebFlux中编写带注释的终结点的方法:

Here's how to write an annotated endpoint in WebFlux:

@RestController public class HomeController { @GetMapping("/test") public ResponseEntity serverResponseMono() { return ResponseEntity .ok() .contentType(MediaType.APPLICATION_JSON) .body(Flux.just("test")); } }

这是现在的功能方式:

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

更多推荐

如何在Postman中查看Spring 5 Reactive API的响应?

本文发布于:2023-10-29 10:13:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Postman   Spring   Reactive   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!