title: Restrict annotation @Notnull and @Size cannot take effect in Spring Bean
body: <p>I want to use annotation to check bean's property, but <code>@NotNull</code> and <code>@Size</code> annotation don't take effect. please see my code below:    </p>
  
  <p>Spitter bean:    </p>
  
  <pre><code>package spittr.data;
  
  import javax.validation.constraints.NotNull;
  import javax.validation.constraints.Size;
  
  public class Spitter {
  
      private int id;
  
      @NotNull
      @Size(min=5, max=25)
      private String password;
  
      @NotNull
      @Size(min=2, max=30)
      private String firstName;
  
      @NotNull
      @Size(min=2, max=30)
      private String lastName;
  
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
      public String getPassword() {
          return password;
      }
      public void setPassword(String password) {
          this.password = password;
      }
      public String getFirstName() {
          return firstName;
      }
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
      public String getLastName() {
          return lastName;
      }
      public void setLastName(String lastName) {
          this.lastName = lastName;
      } 
  
      public String toString()
      {
          return "id:" + id + "\n" +
                 "firstName:" + firstName + "\n"+
                 "lastName:" +lastName+"\n"+
                 "password:" + password;
      }
  }
  </code></pre>
  
  <p>SpitterController:    </p>
  
  <pre><code>package spittr.web;
  
  import javax.validation.Valid;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.validation.Errors;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  
  import spittr.data.Spitter;
  import spittr.data.SpitterRespository;
  
  @Controller
  @RequestMapping("/spitter")
  public class SpitterController {
  
      private SpitterRespository spitterRespository;
      public SpitterRespository getSpitterRespository() {
          return spitterRespository;
      }
  
      @Autowired
      public void setSpitterRespository(SpitterRespository spitterRespository) {
          this.spitterRespository = spitterRespository;
      }
  
      @RequestMapping(value="/register", method=RequestMethod.GET)
      public String showRegistrationForm(Model model)
      {
          model.addAttribute("spitter",new Spitter());
          System.out.println("SpitterController.showRegistrationForm is called");
          return "registForm";
      }
  
      @RequestMapping(value="/register", method=RequestMethod.POST)
      public String processRegistration(@Valid Spitter spitter, Errors errors)
      {
          if (errors.hasErrors())
          {
  
              return "registForm";
          }
          System.out.println("SpitterController.processRegistration is called");
          System.out.println(spitter.toString());
          spitterRespository.saveSpitter(spitter);
  
          return "redirect:/spitter/"+spitter.getId();
      }
  
      @RequestMapping(value="/{userId}", method=RequestMethod.GET)
      public String showSpitterProfile(@PathVariable("userId") String userId, Model model)
      {
          System.out.println("SpitterController.showSpitterProfile is called");
          Spitter spitter = spitterRespository.findSpitterById(userId);
          model.addAttribute("Spitter",spitter);
          return "userprofile";
      }
  }
  </code></pre>
  
  <p>registForm.jsp:    </p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"  %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h1&gt;Register&lt;/h1&gt;
  &lt;sf:form method="POST" modelAttribute="spitter"&gt;
  User ID: &lt;sf:input  path="id" /&gt;&lt;sf:errors path="id" /&gt;   &lt;br /&gt;
  First Name: &lt;sf:input  path="firstName" /&gt;&lt;sf:errors path="firstName" /&gt;  &lt;br/&gt;
  Last Name: &lt;sf:input  path="lastName" /&gt;&lt;sf:errors path="lastName" /&gt; &lt;br/&gt;
  Password: &lt;sf:input  path="password" /&gt;&lt;sf:errors path="password" /&gt; &lt;br/&gt;
  
  &lt;input type="submit" value="Register" /&gt;
  &lt;/sf:form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>the effect that I click on Register button:    </p>
  
  <p><a href="https://i.stack.imgur.com/Wme0z.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Wme0z.jpg" alt="enter image description here"></a></p>
  
  <p>If I type some number in the ID field and keep the others as empty, the validation will be passed, but I think I have set <code>@NotNull</code> and <code>@Size</code> annotation on the Spitter bean, so it's not reasonable for spring to verify it successfully and jump to another page. but in fact, it does, what's wrong with my code?</p>