Java 尝试使用反应式数据时,我得到的属性无效';id';bean类的[reactor.core.publisher.MonoOnAssembly]

Java 尝试使用反应式数据时,我得到的属性无效';id';bean类的[reactor.core.publisher.MonoOnAssembly],java,spring,thymeleaf,spring-webflux,Java,Spring,Thymeleaf,Spring Webflux,我试图使用thymeleaf表单和单值反应式数据流。使用spring boot 2.1.3、thymeleaf 3.0.11和webflux。 我的目标是: @Getter @Setter @NoArgsConstructor public class RecipeCommand { private String id; private String description; } 控制器为: @GetMapping("/recipe/{id}/update") public S

我试图使用thymeleaf表单和单值反应式数据流。使用spring boot 2.1.3、thymeleaf 3.0.11和webflux。 我的目标是:

@Getter
@Setter
@NoArgsConstructor
public class RecipeCommand {
    private String id;
    private String description;
}
控制器为:

@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";
}
@GetMapping(“/recipe/{id}/update”)
公共字符串updateRecipe(模型模型,@PathVariable字符串id){
Mono recipeCommandMono=recipeService.getRecipeCommandById(id);
model.addAttribute(“配方”,recipeCommandMono);
返回“recipes/recipeform”;
}
其中recipes/recipeform是用于更新配方的表单:

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

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

原因:org.springframework.beans.NotReadablePropertyException:bean类[reactor.core.publisher.MonoOnAssembly]的属性“id”无效:bean属性“id”不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配

任何关于如何使用thymeleaf表单和反应流的想法

POM:


4.0.0
org.springframework.boot
spring启动程序父级
2.1.3.1发布
com.gabriel
春药
0.0.1-快照
春药
11
org.springframework.boot
spring启动程序数据mongodb
org.springframework.boot
spring引导启动器数据mongodb
org.springframework.boot
弹簧启动装置
org.springframework.boot
弹簧启动器webflux
org.springframework.boot
弹簧靴开发工具
运行时
org.projectlombok
龙目
真的
org.webjars
独自创立
4.3.1
org.webjars
jquery
3.3.1
org.webjars
popper.js
1.14.7
org.springframework.boot
弹簧起动试验
测试
朱尼特
朱尼特
测试
org.springframework.boot
springbootmaven插件
org.apache.maven.plugins
maven故障保护插件
3.0.0-M3
集成测试
验证

您正试图将Mono传递给模板。相反,您可以使用IReactiveDataDriverContextVariable对象包装Mono。IReactiveDataDriverContextVariable与通量一起工作,因此您应该使用Flux()方法将Mono转换为Flux

@GetMapping(“/recipe/{id}/update”)
公共字符串updateRecipe(模型模型,@PathVariable字符串id){
Mono recipeCommandMono=recipeService.getRecipeCommandById(id);

IReactiveDataDriverContextVariable配方=新的反应型DataDriverContextVariable(recipeCommandMono.flux(),1); model.addAttribute(“配方”,配方); 返回“recipes/recipeform”; }
更改thymeleaf部分(变量配方现在是异步列表)



我切换到spring boot 2.2.2.RELEASE。它解决了这个问题。现在,当作为model属性传递时,thymeleaf接受Mono。控制器和表单模板均无需更改

您可以共享pom.xml文件吗?IReactiveDataDriverContextVariable应该能够像Flux一样包装多值流。尝试包装Mono时出现一个get错误-java.lang.IllegalArgumentException:被动数据驱动程序上下文变量设置为单值异步对象。但是数据驱动变量必须包装多值数据流(这样它们就可以在templateI更新的答案中进行迭代。如果不想使用Flux然后对其进行迭代,可以使用.block()要在使用flux时打开Mono并获取请求的数据,thymeleaf表单中会出现id不可读的错误。阻止也是不可能的,并给出错误java.lang.IllegalStateException:block()/blockFirst()/blockLast()正在阻止,这在thread reactor-http-epoll-1中不受支持否没有block()method.method返回Mono。当您使用IReactiveDataDriverContextVariable时,是否更改了thymeleaf模板?IReactiveDataDriverContextVariable提供的是流,而不是对象
<?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>