spring 对于该类型,未定义getType()方法

qcbq4gxm  于 5个月前  发布在  Spring
关注(0)|答案(2)|浏览(65)
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
 }
}


上面的类是一个独立的类

kxeu7u2r

kxeu7u2r1#

看来你不是第一个遇到这个问题的人https://coderanch.com/t/730026/java/lombok
在类DesignTacoController的addadditientsToModel中,标记的错误是“The constructor subsidient(String,String,subsidient.Type)is undefined”。*此外,在方法filterByType中,标记的错误是“The method getType()is undefined for the type subsidient"。看起来lombok只是不工作。但是我在pom中有lombok:
答:
仅仅将Lombok作为依赖项添加并不能使Eclipse识别它,你需要一个插件。有关将Lombok安装到Eclipse中的说明,请参阅https://www.baeldung.com/lombok-ide(对于喜欢它的人,请参阅IntelliJ)。

zyfwsgd6

zyfwsgd62#

这不是lombok的问题。这是因为在组件类中没有getType()函数。添加getType()函数将解决这个问题。

public class Ingredient {
  
  private final String id;
  private final String name;
  private final Type type;
  
  public Ingredient(String id, String name, Type type) {
        this.id = id;
        this.name = name;
        this.type = type;
      }
  
  public enum Type {
    WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
  }
  
  *public Type getType() {
      return this.type;
  }*

}

字符串

相关问题