I am currently beginning to learn about Spring boot, and how to use the build in validation tools(anotation) in an class' fields. I know how to validate a String and set a field to @NotNull, but cant find any way to do the same with a numerical value. I thought about using @Pattern for regex but hoped there was something more clear(and easier) to use.
I do not want to set a @Min or @Max. It should be able to use all possible numerical int values.
Current code, where i want to set the notation at zipCode(the last field)
@Entity
public class Customer {
@Id
private int id;
@NotNull
@Size(min=2,max=15)
private String firstName;
@NotNull
@Size(min=2,max=15)
private String lastName;
@NotNull
@Size(min=5,max=45)
private String address;
@NotNull
@Size(min=1,max=15)
private String driversLicense;
@NotNull
@Size(min=1, max=3)
private String licensesType;
@NotNull
@Size(min=1,max=30)
private String phone;
@NotNull
@Size(min=1,max=15)
private String nationality;
@NotNull
@Size(min=1,max=30)
private String city;
@NotNull
private int zipCode;
With the @NotNull notation, the error message I get is
Failed to convert property value of type java.lang.String to required type int for property zipCode;
nested exception is java.lang.NumberFormatException: For input string:"xx"
and want it to be a custome error message, but when i do
@NotNull(message="Custom error message")
private int zipCode
I still get the above error message instead.
Any ideas to fix these two problems, would be much appreciated
edit: did not realise that there could only be one "approved" answer to this question. As it's two questions in one, both @Pankaj and @JArgente helped with great answers