I encountered some of the Spring annotation questions in one of the interview questions and I want to quickly cover this with my viewers. I would be discussion various Spring Annotations in this series.
@NotNull
The purpose of this annotation is to make sure that the field is not null and it can be applied to any type of object , the field must be initialized with non null value.
@NotBlank
This annotation ensures that a field is not NULL
, and not empty, and contains at least one non-whitespace character. This is only Applied only to String
fields.
@ModelAttribute
This annotation in Spring MVC is used to bind a method parameter or method return value to a named model attribute, and then expose it to a web view. This annotation can be applied at the method level or on a method parameter.
When used with the request mapping it
indicates that the parameter should be retrieved from the model. If the attribute does not exist in the model, Spring will instantiate it, add it to the model, and then bind request parameters to it.
@Controller
public class UserController {
@RequestMapping("/register")
public String showRegistrationForm(@ModelAttribute("user") User user) {
return "register";
}
}