Spring in action 5- chapter 3 issue

Spring in action 5- chapter 3 issue

You need to generate the list of ingredients in case of error (again).

    public String designForm(@ModelAttribute(design) @Valid Taco taco, Errors errors, Model model) {
        if (errors.hasErrors()) {
            List<Ingredient> ingredients = ingredientRepository.findAll();

            Type[] types = Ingredient.Type.values();
            for (Type x : types) {
                         ingredients.stream().filter(p -> p.getType().equals(x)).collect(Collectors.toList()));
            return design;
        log.info(Processing desing:  + taco);

        return redirect:/order/current;


model.addAttribute(order,new Order()); 

in OrderController would fix the issue.
TacoController is passing the model with order instance populated, however the get method in order controller, specifically the above line add a new order to model, overwriting the old order details.

Spring in action 5- chapter 3 issue

I too am going over Spring in Action 5, chapter 3. I had an issues with my ingredients field being null as well. I have managed to fix this.

Somewhere in Ch3, the code for ingredients changed types. In your Taco class, ingredients should be List (as seen from ch2); however this changed to List in Ch3 without any mention. I found out about that change looking at the chapters source code. Anyhow, because the lists type switched from String to Ingredient, Spring will no longer bind the list of ingredients from the form.

My fix is this:
1 – revert the Taco class ingredient field to be a List data type.
2 – in the JdbcTacoRepository class, make the following changes:
2a – add a new private final field of type IngredientRepository
2b – update the constructor to autowire the above field
2c – within the save(Taco taco) method, use the following changes:

for(String ingredientId : taco.getIngredients()){ Ingredient ingredient = ingredientRepo.findOne(ingredientId); saveIngredientToBurger(ingredient, burgerId); }

Overall the ingredients field was null b/c Spring couldnt bind a String object to an Ingredient object. Im still trying to understand how Spring recognizes the ingredient in the first place, but hopefully this works for you.

Leave a Reply

Your email address will not be published. Required fields are marked *