Issue
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());
}
}
I was going through the book Spring in action edition 6 chapter. In that in the filterByType method the '.getType()' is showing the error
The method getType() is undefined for the type Ingredient
I thought it was the error due to lombok but I have installed that as well. I have also import the package 'java.lang.reflect.Field' but still getting the error.
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
}
}
The above class is the Ingredient Class
Solution
Seems you are not the first person who faced with this issue https://coderanch.com/t/730026/java/lombok
In addIngredientsToModel of class DesignTacoController the flagged error is "The constructor Ingredient(String, String, Ingredient.Type) is undefined". Also, in method filterByType the flagged error is "The method getType() is undefined for the type Ingredient". It zappears that lombok is just not working. But I have lombok in the pom:
Answer:
Just adding Lombok as a dependency does not make Eclipse recognize it, you'll need a plugin for that. See https://www.baeldung.com/lombok-ide for instructions on installing Lombok into Eclipse (and IntelliJ for those who prefer it).
Answered By - Alexey Semenyuk