java - 当尝试使用 react 数据时,我得到了 bean 类 [reactor.core.publisher.MonoOnAssembly] 的无效属性 'id'

标签 java spring thymeleaf spring-webflux

我正在尝试使用 thymeleaf 形式以及单值 react 数据流。使用 spring boot 2.1.3、thymeleaf 3.0.11 和 webflux。 我的 POJO 是:

@Getter
@Setter
@NoArgsConstructor
public class RecipeCommand {
    private String id;
    private String description;
}

Controller 是:

@GetMapping("/recipe/{id}/update")
public String updateRecipe(Model model, @PathVariable String id){

    Mono<RecipeCommand> recipeCommandMono = recipeService.getRecipeCommandById(id);
    model.addAttribute("recipe", recipeCommandMono);
    return "recipes/recipeform";
}

其中recipes/recipeform是 thymeleaf 形式来更新菜谱:

<form  th:object="${recipe}" th:action="@{/post}" method="post">
    <input type="hidden" th:field="*{id}"/>

应用程序运行,但在尝试更新配方时出现以下错误:

Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'id' of bean class [reactor.core.publisher.MonoOnAssembly]: Bean property 'id' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

知道如何将 thymeleaf 形式与 react 流一起使用。

POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gabriel</groupId>
    <artifactId>spring-recipe</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-recipe</name>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>popper.js</artifactId>
            <version>1.14.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

最佳答案

您正在尝试将 Mono 传递给您的模板。相反,您可以使用 IReactiveDataDriverContextVariable 对象包装 Mono 。 IReactiveDataDriverContextVariable 与 Flux 配合使用,因此您应该使用 flux() 方法将 Mono 转换为 Flux

@GetMapping("/recipe/{id}/update")
public String updateRecipe(Model model, @PathVariable String id){

    Mono<RecipeCommand> recipeCommandMono = recipeService.getRecipeCommandById(id);
    IReactiveDataDriverContextVariable recipe = new ReactiveDataDriverContextVariable(recipeCommandMono.flux(), 1);
    model.addAttribute("recipes", recipe);
    return "recipes/recipeform";
}

更改 thymeleaf 部分(变量食谱现在是异步列表)

<div th:each="recipe : ${recipes}">
    <form  th:object="${recipe}" th:action="@{/post}" method="post">
        <input type="hidden" th:field="*{id}"/>

关于java - 当尝试使用 react 数据时,我得到了 bean 类 [reactor.core.publisher.MonoOnAssembly] 的无效属性 'id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59555315/

相关文章:

java外部线程(在它使用的类文件之外)

java - 如何从多个字符串中解析文本?

java - 运行项目时 "Unsatisfied dependency expressed through constructor parameter 0"

spring boot + thymeleaf 无法正确编码 POST 形式的 UTF-8 字符

spring-boot - thymeleaf 片段渲染后重新加载 javascript

java - 如何使用 Selenium 获取 WebElement 文本

java - 如何从给定的输入中标记一组行?

java - Spring JPA Repository - 在服务器重启时保留数据

spring-security - Spring Boot + Security + Thymeleaf 和 CSRF token 未自动注入(inject)

authentication - 登录尝试失败后将用户名保留在表单输入字段中(Java/Spring-security)