Issue
I am using
SpringToolSuite
as IDE and developing usingSpring MVC
.In the model part of my application I am defining a bean called Ingredient
import lombok.Data;
@Data
public class Ingredient {
public Ingredient(String string, String string2, Type wrap) {
// TODO Auto-generated constructor stub
}
private String id;
private String name;
private Type type;
public enum Type{
WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
}
}
As I am using the
@Data
annotation from Lombok I can suppose that a constructor taking the 3 attributes is automatically created as well as getter and setters.But as in the controller I call
new Ingredient("FLTO", "Flour Tortilla", Type.WRAP)
I get a redline under the instruction with a message that tells me that there is no constructor with the parameters in question.- I don't understand because the class Ingredient is marked with the Lombok's annotation
@Data
- I don't understand because the class Ingredient is marked with the Lombok's annotation
The errors cause the running of the SpringBoot's project to crash
Caused by: java.lang.Error: Unresolved compilation problems:
The method asList(T...) in the type Arrays is not applicable for the arguments (Ingredient, Ingredient, Ingredient, Ingredient, Ingredient, Ingredient, Ingredient, Ingredient, Ingredient, Ingredient)
The constructor Ingredient(String, String, Ingredient.Type) is undefined
.........................
Solution
@Data
documentation
Equivalent to @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode.
As your fields are not marked as final
they are not covered by the @RequiredArgsConstructor
Therefor
- Declare the required fields as
final
or - Use
@AllArgsConstructor
Edit
@Data
public class Ingredient {
private final String id;
private final String name;
private final Type type;
public enum Type{
WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
}
}
gets delomoked to:
@Data
public class Ingredient {
private final String id;
private final String name;
private final Type type;
@java.beans.ConstructorProperties({"id", "name", "type"})
public Ingredient(String id, String name, Type type) {
this.id = id;
this.name = name;
this.type = type;
}
....
}
And then I can use
Ingredient test = new Ingredient("id", "name", Ingredient.Type.CHEESE);
Answered By - Daniel Wosch
Answer Checked By - Pedro (JavaFixing Volunteer)