java - 是否可以从 GetMapping ("index")处理程序返回 Mono.just ("/")?

标签 java spring-webflux project-reactor

让我们考虑这个请求处理程序:

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

如果templates目录中有index.html模板,则不会执行此处理程序。

如果我从处理程序返回 Mono.just("index"):

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

该处理程序仍未使用。

看来我必须从 templates 目录中删除 index.html 模板,才能将 GetMapping("/") 路由到予以处理。

所以我的问题是,是否可以从 GetMapping("/") 处理程序返回 Mono.just("index")

最佳答案

据此book ,您应该使用@controller代替@RestController。

这是另一个样本:

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

对应的thymeleaf html页面:

<!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> 

关于java - 是否可以从 GetMapping ("index")处理程序返回 Mono.just ("/")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62550355/

相关文章:

java - 带有 .java 的 Netbeans Jar 文件,而不是 .class 文件

spring - 为什么我的 Spring WebFilter 没有应用于我的 webclient API 请求?

Spring Boot Webflux - 设置UTF-8编码

java - 使用 MySQL 触发器调用 Java 方法

java - 插入到已排序的 LinkedList Java

java - 在 linux 上拦截 HTTP 请求

spring - 如何在 Spring WebClient 中管理/创建连接池?

java - 在 Spring 5 Webflux 中启用 CORS?

java - 总是调用 Mono switchIfEmpty()

spring - 如何避免Spring Reactor中类似订阅者重复映射操作?