无法将java.lang.String[]类型的属性值转换为所需的java.util.List类型
我正在跟随Spring的动作5,在按下submit按钮后创建Taco模型时遇到问题。这是我设计的Taco控制器类:无法将java.lang.String[]类型的属性值转换为所需的java.util.List类型,java,spring,Java,Spring,我正在跟随Spring的动作5,在按下submit按钮后创建Taco模型时遇到问题。这是我设计的Taco控制器类: @GetMapping public String showDesignForm(Model model){ List<Ingredient> ingredients = new ArrayList<>(); ingredientRepository.findAll().forEach(i -> ingredients.add(
@GetMapping
public String showDesignForm(Model model){
List<Ingredient> ingredients = new ArrayList<>();
ingredientRepository.findAll().forEach(i -> ingredients.add(i));
Type[] types = Ingredient.Type.values();
for (Type type : types){
model.addAttribute(type.toString().toLowerCase(),
filterByType(ingredients, type));
}
return "welcomePage";
}
@ModelAttribute(name = "taco")
public Taco taco(){
return new Taco();
}
@PostMapping
public String processDesign(@Valid Taco taco, Errors errors, @ModelAttribute Order order){
if(errors.hasErrors()) {
return "welcomePage";
}
Taco saved = tacoRepository.save(taco);
order.addDesign(saved);
return "redirect:/orders/current";
}
塔科实体看起来像:
@Data
@Entity
public class Taco {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private Date createdAt;
@NotNull
@Size(min = 3, message="Name must be at least 3 characters long")
private String name;
@ManyToMany(targetEntity = Ingredient.class)
@Size(min=1, message="You must choose at least 1 ingredient")
private List<Ingredient> ingredients = new ArrayList<>();
@PrePersist
void createdAt(){
this.createdAt = new Date();
}
}
这是一个html页面,它必须使用精选的成分创建新的Taco对象:
@Data
@RequiredArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@Entity
public class Ingredient {
@Id
private final String id;
private final String name;
@Enumerated(EnumType.STRING)
private final Type type;
public static enum Type{
WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
}
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Testing Firs Page</title>
</head>
<body>
<h1>Design your taco!</h1>
<img th:src="@{/images/taco.jpg}" alt="myImage"/>
<form method="POST" th:object="${taco}">
<span class="validationError"
th:if="${#fields.hasErrors('ingredients')}"
th:errors="*{ingredients}">Ingredient Error</span>
<div class="grid">
<div class="ingredient-group" id="wraps">
<h3>Designate your wrap:</h3>
<div th:each="ingredient : ${wrap}">
<input name="ingredients" type="checkbox" th:value="${ingredient.id}" />
<span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>
</div>
<div class="ingredient-group" id="proteins">
<h3>Pick your protein:</h3>
<div th:each="ingredient : ${protein}">
<input name="ingredients" type="checkbox" th:value="${ingredient.id}" />
<span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>
</div>
<div class="ingredient-group" id="cheeses">
<h3>Choose your cheese:</h3>
<div th:each="ingredient : ${cheese}">
<input name="ingredients" type="checkbox" th:value="${ingredient.id}" />
<span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>
</div>
<div class="ingredient-group" id="veggies">
<h3>Determine your veggies:</h3>
<div th:each="ingredient : ${veggies}">
<input name="ingredients" type="checkbox" th:value="${ingredient.id}" />
<span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>
</div>
<div class="ingredient-group" id="sauces">
<h3>Select your sauce:</h3>
<div th:each="ingredient : ${sauce}">
<input name="ingredients" type="checkbox" th:value="${ingredient.id}" />
<span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>
</div>
</div>
<div>
<h3>Name your taco creation:</h3>
<input type="text" th:field="*{name}"/>
<span class="validationError"
th:if="${#fields.hasErrors('name')}"
th:errors="*{name}">Name Error</span>
<br/>
<button>Submit your taco</button>
</div>
</form>
</body>
</html>
测试第一页
设计你的墨西哥玉米卷!
成分误差
指定您的包裹:
成分
选择你的蛋白质:
成分
选择你的奶酪:
成分
确定你的蔬菜:
成分
选择您的酱汁:
成分
命名您的塔可制作:
名称错误
提交你的玉米卷
我怎样才能修好它?感谢提前通知。错误是:
无法将属性成分[0]
的类型java.lang.String
的值转换为所需的类型org.server.models.component
您没有共享Taco
或component
的代码,也没有共享POST
请求的有效负载,因此我们无法确定您需要更改什么
但是,如果您将构造函数添加到component
中,并使用String
参数,我相信Spring会使用它
当然,如何从字符串
值创建成分
对象将取决于字符串内容是什么,因此这完全取决于您自己。如果您需要这方面的帮助,请创建一个新问题,并包含相关信息,例如POJO类的代码和POST
请求的内容。错误为:
无法将属性成分[0]
的类型java.lang.String
的值转换为所需的类型org.server.models.component
您没有共享Taco
或component
的代码,也没有共享POST
请求的有效负载,因此我们无法确定您需要更改什么
但是,如果您将构造函数添加到component
中,并使用String
参数,我相信Spring会使用它
当然,如何从
字符串
值创建成分
对象将取决于字符串内容是什么,因此这完全取决于您自己。如果您需要这方面的帮助,请创建一个新问题,并包括相关信息,例如POJO类的代码和POST
请求的内容。在Spring In Action中,您应该添加IngReditionByIDConverter类。该类正在将成分转换为字符串
@Component
public class IngredientByIdConverter
implements Converter<String, Ingredient> {
private IngredientRepository ingredientRepo;
@Autowired
public IngredientByIdConverter(IngredientRepository ingredientRepo) {
this.ingredientRepo = ingredientRepo;
}
@Override
public Ingredient convert(String id) {
return ingredientRepo.findById(id);
}
}
@组件
公共类IngCreditByIDConverter
机具转换器{
私人IngredEntrepository IngredEnterpo;
@自动连线
公共InCreditByIDConverter(IngredEnterpository IngredEnterpo){
this.IngredEnterpo=IngredEnterpo;
}
@凌驾
公共成分转换(字符串id){
返回IngredEnterpo.findById(id);
}
}
在Spring In Action中,您应该添加IngredientByIdConverter类。该类正在将成分转换为字符串
@Component
public class IngredientByIdConverter
implements Converter<String, Ingredient> {
private IngredientRepository ingredientRepo;
@Autowired
public IngredientByIdConverter(IngredientRepository ingredientRepo) {
this.ingredientRepo = ingredientRepo;
}
@Override
public Ingredient convert(String id) {
return ingredientRepo.findById(id);
}
}
@组件
公共类IngCreditByIDConverter
机具转换器{
私人IngredEntrepository IngredEnterpo;
@自动连线
公共InCreditByIDConverter(IngredEnterpository IngredEnterpo){
this.IngredEnterpo=IngredEnterpo;
}
@凌驾
公共成分转换(字符串id){
返回IngredEnterpo.findById(id);
}
}
请您也发布错误跟踪。上面的代码片段是否来自两个不同的文件(控制器和模型)?如果是,请相应地分割片段。Syed Affan Hamdani我编辑了错误消息并添加了墨西哥玉米卷,配料entities@Scroll您仍然没有向我们显示POST
有效负载,根据错误消息,该有效负载只有一个字符串用于成分
字段。您希望字符串如何将字符串映射到列表
?@Andreas我已经用方法添加了html页面POST@Scroll因此成分的值是成分ID的列表。当您没有以任何方式通知Spring字符串是简单的成分ID时,您希望Spring如何处理字符串到成分
对象的映射?你以为春天会猜吗?重新思考你在做什么。你能不能也发布错误跟踪。上面的代码片段是否来自两个不同的文件(控制器和模型)?如果是,请相应地分割片段。Syed Affan Hamdani我编辑了错误消息并添加了墨西哥玉米卷,配料entities@Scroll您仍然没有向我们显示POST
有效负载,根据错误消息,该有效负载只有一个字符串用于成分
字段。您希望字符串如何将字符串映射到列表
?@Andreas我已经用方法添加了html页面POST@Scroll因此成分的值是成分ID的列表。当您没有以任何方式通知Spring字符串是简单的成分ID时,您希望Spring如何处理字符串到成分
对象的映射?你以为春天会猜吗?我已经编辑了错误信息并添加了taco,配料实体我已经编辑了错误信息并添加了taco,配料实体