Is it possible to return a Mono.just("index") from a GetMapping("/") handler?

Viewed 248

Let's consider this request handler:

@GetMapping("/")
public Mono<String> index(Model model) {
  model.addAttribute("list", Flux.just("item1", "item2"));
  return Mono.just("list"); // <- Template name
}

If there is an index.html template in the templates directory, this handler is not excuted.

If I return Mono.just("index") from the handler:

@GetMapping("/")
public Mono<String> index(Model model) {
  model.addAttribute("list", Flux.just("item1", "item2"));
  return Mono.just("index"); // <- Template name
}

This handler is still not used.

It seems like I have to remove the index.html template from the templates directory for the GetMapping("/") route to be handled.

So my question is, is it possible to return a Mono.just("index") from a GetMapping("/") handler ?

1 Answers

As per this book, you should use @Controller instead of @RestController.

And here is another sample:

@GetMapping("/welcome")
    public Mono<String> hello(final Model model) {
        model.addAttribute("name", "Foo");
        model.addAttribute("city", "Bar");

        String path = "hello";
        return Mono.create(monoSink -> monoSink.success(path));
    }

The corresponding thymeleaf html page:

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8"/>
    <title>Greet</title>
</head>

<body>

<p th:text="${name}"></p> lives in <p th:text="${city}"></p> 

</body>
</html> 
Related