Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i turn springboot rest endpoints to spring webflux reactive endpoints

bellow is my code, its written in springboot but im having trouble trying to write it in webflux. im using mono but im not sure how i can log info in mono or how i can return a responseEntity using mono either.

@GetMapping("/helloWorld")
public ResponseEntity helloWorld() {

    log.info("Entered hello world");

    return ResponseEntity.ok("Hello World");
}

there is not much i did in webflux but this is how far i got

   @GetMapping("/helloWorld")
    public Mono<ResponseEntity> helloWorld() {
//        log.info("Entered hello world in controller");
        Mono<String> defaultMono = Mono.just("Entered hello world").log();
        defaultMono.log();
//        return ResponseEntity.ok("Hello World ");

    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

@GetMapping("/helloWorld")
public Mono<ResponseEntity<String>> helloWorld() {

   log.info("Entered hello world");

   return Mono.just(ResponseEntity.ok("Hello World"));
}

May suggest reading the spring webflux docs as well Project Reactor docs.

Also if what you need is to log all the reactive stream I’d would suggest following code.

@GetMapping("/helloWorld")
public Mono<ResponseEntity<String>> helloWorld() {       
    return Mono.just("Entered hello world in controller").log().map(s -> ResponseEntity.ok(s));
}

Some good spring webflux example can be found on internet, such: Building Async REST APIs with Spring WebFlux

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading