Reputation: 393
I am new to Spring and Spring Boot and am working through a book that is full of missing information.
I have a taco class:
public class Taco {
...
@Size(min=1, message="You must choose at least 1 ingredient")
private List<Ingredient> ingredients;
...
}
As you can see ingredients
is of type List<Ingredient>
and that is the problem, it used to be of type List<String>
but that was before I started saving data in the database, now it must be List<Ingredient>
which breaks the whole thing.
In the contoller I have the following (among other things, I think this is the only required code for the problem at hand but if you need more let me know):
@ModelAttribute
public void addIngredientsToModel(Model model) {
List<Ingredient> ingredients = new ArrayList<>();
ingredientRepo.findAll().forEach(i -> ingredients.add(i));
Type[] types = Ingredient.Type.values();
for (Type type : types) {
model.addAttribute(type.toString().toLowerCase(), filterByType(ingredients, type));
}
}
private List<Ingredient> filterByType(List<Ingredient> ingredients, Type type) {
return ingredients
.stream()
.filter(x -> x.getType()
.equals(type))
.collect(Collectors.toList());
}
And finally in my thymeleaf file I have:
<span class="text-danger"
th:if="${#fields.hasErrors('ingredients')}"
th:errors="*{ingredients}">
</span>
Which causes the error:
thymeleaf Failed to convert property value of type java.lang.String to required type java.util.List
Once again, when private List<Ingredient> ingredients;
was private List<String> ingredients;
it worked, but now it must be private List<Ingredient> ingredients;
because of the way it is saved in the database but it breaks at this point, how to fix it?
Upvotes: 4
Views: 4320
Reputation: 1830
An optimisation of Ian's answer above:
Fetch the ingredients in the constructor of the converter.
package com.joeseff.xlabs.chtp01_1.tacos.converter;
import com.joeseff.xlabs.chtp01_1.tacos.model.Ingredient;
import com.joeseff.xlabs.chtp01_1.tacos.respository.jdbc.IngredientRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @author - JoeSeff
* @created - 23/09/2020 13:41
*/
@Component
public class IngredientConverter implements Converter<String, Ingredient> {
private final IngredientRepository ingredientRepo;
private final List<Ingredient> ingredients = new ArrayList<>();
@Autowired
public IngredientConverter(IngredientRepository ingredientRepo) {
this.ingredientRepo = ingredientRepo;
ingredientRepo.findAll().forEach(ingredients::add);
}
@Override
public Ingredient convert(String ingredientId) {
return ingredients
.stream().filter( i -> i.getId().equals(ingredientId))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Ingredient with ID '" + ingredientId + "' not found!"));
}
}
Upvotes: 0
Reputation: 66
I met the same problem, what we need here is a converter.
package tacos.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import tacos.Ingredient;
import tacos.data.IngredientRepository;
@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.findOne(id);
}
}
Upvotes: 3