首页 > 解决方案 > 无法理解为什么此方法无法解决

问题描述

我正在用 Spring In Action 书学习 Spring,我有书中的代码,它可以正常工作:

@Slf4j
@Controller
@RequestMapping("/design")
public class DesignTacoController {
    @GetMapping
    public String showDesignForm(Model model) {
        List<Ingredient> ingredients = Arrays.asList(
                new Ingredient("FLTO", "Flour Tortilla", Type.WRAP),
                new Ingredient("COTO", "Corn Tortilla", Type.WRAP),
                new Ingredient("GRBF", "Ground Beef", Type.PROTEIN),
        );

        Type[] types = Ingredient.Type.values();
        for (Type type : types) {
            model.addAttribute(type.toString().toLowerCase(),
                    filterByType(ingredients, type));
        }

        model.addAttribute("design", new Taco());
        return "design";
    }
} 

但是当我在我的 IDEA 中输入它时,它说方法 filterByType 无法解决,但书中没有这样的问题,也没有关于这个问题的任何评论。我是春天的新手,尝试了很多谷歌,但找不到有关此问题及其来源的任何信息。你能帮我解决这个问题吗,因为它不能更进一步。 IDEA的截图

标签: javaspring

解决方案


看起来这本书只是包含一个错误,它没有列出filterByType()方法。这不是 Spring 方法。干得好:

private List<Ingredient> filterByType(List<Ingredient> ingredients, Type type) {

    return ingredients.stream()
            .filter(x -> x.getType().equals(type))
            .collect(Collectors.toList());

}

资料来源:曼宁出版社


推荐阅读