gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:37:44 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com