title: How to return 400 HTTP error code when some property of a RequestBody parameter is null?
body: <p>I have the following example:</p>
  
  <p>This is the request body:</p>
  
  <pre><code>public class UserLoginData
      implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      private String username;
      private String password;
      //... getter and setters
  }
  </code></pre>
  
  <p>This is the Controller:</p>
  
  <pre><code>@RequestMapping(value = {"/login"}, method = RequestMethod.POST)
  @ResponseBody
  public LoginResponse login(@RequestBody(required = true) UserLoginData loginData){
      //... some code
   }
  </code></pre>
  
  <p>This is how I invoke the service:</p>
  
  <pre><code>POST /login
  {"username":"neuquino"}
  </code></pre>
  
  <p>I expect that Spring returns a HTTP 400 BAD REQUEST error, because password is missing. But instead of that, it returns a HTTP 500 INTERNAL SERVER error with the following stacktrace:</p>
  
  <pre><code>java.lang.NullPointerException
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
  at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
      ...
  </code></pre>
  
  <p><strong>How can I specify to Spring that username and password are required fields in request body?</strong></p>