【问题标题】:How to view response from Spring 5 Reactive API in Postman?如何在 Postman 中查看 Spring 5 Reactive API 的响应?
【发布时间】:2017-11-18 03:18:51
【问题描述】:

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

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

目前我可以在 Postman 中看到文本 "data:"Content-Type →text/event-stream。据我了解Mono&lt;ServerResponse&gt; 始终使用SSE(Server Sent Event) 返回数据。 是否可以在 Postman 客户端中以某种方式查看响应?

【问题讨论】:

  • 您好,您确定您收到的回复是使用邮递员以外的其他消费者吗?
  • @Seb 其实我只是在玩代码。在文档中看到了这个例子。
  • Mono&lt;ServerResponse&gt; 并不总是将数据作为 SSE 返回。这要么是 WebFlux 错误,要么是您的设置有问题。你能用最新的里程碑再试一次,或者用示例项目在 jira.spring.io 上打开一个问题吗?
  • @BrianClozel 我使用start.spring.io 创建了项目。选择最新的spring bootreactive web。用我上面写的一个端点添加了休息控制器。
  • 这样的项目没有端点,所以你可能会添加更多。请创建一个示例项目和一个 JIRA 问题。

标签: spring postman spring-webflux


【解决方案1】:

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

以下是如何在 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);
    }

}

【讨论】:

    猜你喜欢
    • 2017-01-15
    • 2020-07-26
    • 2018-09-13
    • 1970-01-01
    • 2018-08-22
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    相关资源
    最近更新 更多