Spring webflux


Mono

  • Mono is a publisher interface given by spring web flux.
  • Mono returns 0 or 1 element.
  • Mono supports generic type, we can declare the type of object return.
  • We can use Mono as a return type for controllers also.
  • Mono is a publisher stream for emitting elements from it.
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import reactor.core.publisher.Mono;

@Controller
public class MyController {

	@GetMapping("/test")
	@ResponseBody
	public Mono<String> test() throws Exception {
		return Mono.just("Hi");
	}

}

Flux

  • Flux is a publisher interface given by spring web flux.
  • Flux returns 0 to n elements.
  • Flux supports generic type, we can declare the type of objects return.
  • We can use Flux as a return type for controllers also.
  • Flux is a publisher stream for emitting elements from it.
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Controller
public class MyController {

	@GetMapping("/test")
	@ResponseBody
	public Flux<String> test() throws Exception {
		return Flux.just("Hi");
	}

}