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. As I understand Mono<ServerResponse>
always return data with SSE(Server Sent Event)
.
Is it possible to somehow view response in Postman client?
Write your first Postman test First thing in the right you can see a select menu with test already create by Postman. We can select one already Status code: Code is 200 . If you click send now we can see the test pass 1/1 . Now add the Response body: JSON value check again in your right snippets.
Spring Reactive Web: The spring reactive web provides a reactive feature to our application. Spring Data R2DBC: Provides Reactive Relational Database Connectivity to persist data in SQL stores using Spring Data in reactive applications. Lombok: Java annotation library which helps to reduce boilerplate code.
To get a Postman API key, you can generate one in the API keys section in your Postman account settings. An API key tells the API server that the received request from you. Everything that you have access to in Postman is accessible with your API key.
It seems you're mixing the annotation model and the functional model in WebFlux. The ServerResponse
class is part of the functional model.
Here's how to write an annotated endpoint in 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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With