简体   繁体   English

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

[英]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.

相关问题 在 Spring Reactive WebFlux 中从外部 API 获取响应 - Get response from external API in Spring Reactive WebFlux 如何获得助焊剂 <T> 来自Mono <K> 在Spring Reactive API中? - How to get Flux<T> from Mono<K> in Spring Reactive API? 反应式响应体 spring - Response body in reactive spring 如何使用WebClient使用Reactive Spring Rest API - How to consume a Reactive Spring Rest API with WebClient 如何在 Spring Reactive Websocket API 中广播消息? - How to broadcast messages in Spring Reactive Websocket API? 如何阻止从 Postman/其他 API 的/等访问我的 API .. (Spring Boot) - How to block access to my API from Postman/Other API's/etc.. (Spring Boot) Spring Reactive API 响应空白主体同时转换 Mono<void> 至 Mono<response></response></void> - Spring Reactive API response blank body while transforming Mono<Void> to Mono<Response> 如何在反应性 spring 数据弹性搜索 api 中更改 elasticsearch 版本? - How to change elasticsearch version in reactive spring data elastic search api? 如何使用 Spring Boot Web Authentication 和 Postman 测试 API? - How to test API with Spring Boot Web Authentication and Postman? 如何在rest控制器spring boot中为邮递员生成api-key - how to generate api-key for postman in rest controller spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM