Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view response from Spring 5 Reactive API in Postman?

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?

like image 378
Orest Avatar asked Jun 15 '17 14:06

Orest


People also ask

How can I test my localhost API in Postman?

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.

Which is the reactive library for consuming restful webservices in spring?

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.

How do you hit API in Postman?

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.


1 Answers

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

}
like image 92
Brian Clozel Avatar answered Oct 24 '22 23:10

Brian Clozel