【问题标题】:The method getType() is undefined for the type Ingredient未定义类型成分的方法 getType()
【发布时间】:2021-09-12 15:28:32
【问题描述】:
package tacos.web;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import java.lang.reflect.Field;
import lombok.extern.slf4j.Slf4j;
import tacos.Ingredient;
import tacos.Ingredient.Type;
import tacos.Taco;

@Slf4j
@Controller
@RequestMapping("/design")
@SessionAttributes("tacoOrder")
public class DesignTacoController {

@ModelAttribute
public void addIngredientsToModel(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),
          new Ingredient("CARN", "Carnitas", Type.PROTEIN),
          new Ingredient("TMTO", "Diced Tomatoes", Type.VEGGIES),
          new Ingredient("LETC", "Lettuce", Type.VEGGIES),
          new Ingredient("CHED", "Cheddar", Type.CHEESE),
          new Ingredient("JACK", "Monterrey Jack", Type.CHEESE),
          new Ingredient("SLSA", "Salsa", Type.SAUCE),
          new Ingredient("SRCR", "Sour Cream", Type.SAUCE)
        );

        Type[] types = Ingredient.Type.values();
        for (Type type : types) {
          model.addAttribute(type.toString().toLowerCase(),
              filterByType(ingredients, type));
        }
}
  @GetMapping
  public String showDesignForm(Model model) {
    model.addAttribute("taco", new Taco());
    return "design";
  }

  private Iterable<Ingredient> filterByType(
      List<Ingredient> ingredients, Type type) {
    return ingredients
              .stream()
              .filter(x -> x.getType().equals(type))
              .collect(Collectors.toList());
  }

}

我正在阅读《Spring in action》第 6 章这本书。在 filterByType 方法中,'.getType()' 显示错误

The method getType() is undefined for the type Ingredient

我以为是 lombok 导致的错误,但我也安装了它。我还导入了包“java.lang.reflect.Field”,但仍然出现错误。

package tacos;

import lombok.Data;


@Data
public class Ingredient {
 public Ingredient(String string, String string2, Type wrap) {
        // TODO Auto-generated constructor stub
    }

private final String id = "";
 private final String name = "";
 private final Type type = null;
 
 public  enum Type {
     WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
 }
}

上面的类是成分类

【问题讨论】:

  • 您可以发布指向可克隆存储库的链接吗?
  • 请发帖Ingredient类,你说是用lombok注解的?
  • @pleft 我已经发布了成分类。
  • 这个错误是否显示在您的 IDE(以及它是哪个)上,或者当您尝试从命令行编译/运行项目时?例如。如果是从 cmd mvn clean package 运行的 maven 项目?
  • 错误显示在我的 IDE 上。 @pleft

标签: java spring spring-boot spring-mvc


【解决方案1】:

看来你不是第一个遇到这个问题的人https://coderanch.com/t/730026/java/lombok

在 DesignTacoController 类的 addIngredientsToModel 中标记 错误是“构造函数成分(字符串,字符串,成分。类型) 未定义”。另外,在方法 filterByType 中,标记的错误是“The 方法 getType() 未定义类型成分“。它会出现 龙目岛只是不工作。但我在 pom 中有龙目岛:

答案:

只是将 Lombok 添加为依赖项不会让 Eclipse 识别它, 你需要一个插件。见https://www.baeldung.com/lombok-ide 有关将 Lombok 安装到 Eclipse(和 IntelliJ for 喜欢的人)。

【讨论】:

  • 但是我已经在 STS 中安装了 Lombok
  • 似乎我在 pom 文件中添加的依赖项有问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-23
  • 2016-06-07
  • 2021-06-19
  • 2014-07-14
  • 1970-01-01
相关资源
最近更新 更多