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

Ore*_*est 3 spring postman spring-webflux

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

@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());
}
Run Code Online (Sandbox Code Playgroud)

目前我可以看到文本"data:"作为一个正文和Content-Type ?text/event-stream邮差.据我所知,Mono<ServerResponse>总是返回数据SSE(Server Sent Event).有可能以某种方式查看Postman客户端的响应吗?

Bri*_*zel 5

看来你正在混合注释模型和WebFlux中的功能模型.的ServerResponse类是功能性模型的一部分.

以下是如何在WebFlux中编写带注释的端点:

@RestController
public class HomeController {

    @GetMapping("/test")
    public ResponseEntity serverResponseMono() {
        return ResponseEntity
                .ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(Flux.just("test"));
    }
}
Run Code Online (Sandbox Code Playgroud)

这是现在的功能方式:

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

}
Run Code Online (Sandbox Code Playgroud)