如何查看Postman中Spring 5 Reactive API的响应?
作者:互联网
我的应用程序中有下一个端点:
@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→文本/事件流.据我了解Mono< ServerResponse>始终使用SSE(服务器发送事件)返回数据.
有可能以某种方式查看Postman客户端的响应吗?
解决方法:
看来你正在混合注释模型和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);
}
}
标签:spring-webflux,spring,postman 来源: https://codeday.me/bug/20190828/1751783.html