如何在 Postman 中查看来自 Spring 5 Reactive API 的响应?
How to view response from Spring 5 Reactive API in Postman?
我的应用程序中有下一个端点:
@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<ServerResponse>
总是 return 数据与 SSE(Server Sent Event)
。
是否有可能以某种方式在 Postman 客户端中查看响应?
您似乎在 WebFlux 中混合了注释模型和功能模型。 ServerResponse
class 是功能模型的一部分。
以下是如何在 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);
}
}
我的应用程序中有下一个端点:
@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<ServerResponse>
总是 return 数据与 SSE(Server Sent Event)
。
是否有可能以某种方式在 Postman 客户端中查看响应?
您似乎在 WebFlux 中混合了注释模型和功能模型。 ServerResponse
class 是功能模型的一部分。
以下是如何在 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);
}
}