title: Return response code as 400 when @notnull validation fails
body: <p>I'm using javax and Hibernate implementation for validation of my request payload.</p>
  
  <p>version - org.hibernate:hibernate-validator:5.4.1.final</p>
  
  <p><strong>Sample Pojo:</strong></p>
  
  <pre><code>import javax.validation.constraints.NotNull;
  import javax.validation.constraints.Size;
  import java.io.Serializable;
  
      public class Customer implements Serializable{
  
          @NotNull(message="name is null")
          @Size(min = 1)
          public String name;
          @NotNull(message="country is null")
          @Size(min = 1)
          public String country;
      }
  </code></pre>
  
  <p><strong>Sample controller:</strong></p>
  
  <pre><code>    @RequestMapping(value = {"/customer"}, method = RequestMethod.POST)
      @ResponseBody
      public Customer doAdd(@RequestBody @Valid Customer inData){
          //some logic
  return outData
      }
  </code></pre>
  
  <p><strong>Sample input json</strong>:</p>
  
  <pre><code>{
  "country":"canada"
  }
  </code></pre>
  
  <p><strong>Console Exception:</strong></p>
  
  <pre><code>org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument at index 0 in method: ........ model.request.Customer,java.lang.String,java.lang.String,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse), with 1 error(s): [Field error in object 'Customer' on field 'name': rejected value [null]; codes [NotNull.Customer.name,NotNull.name,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [Customer.name,name]; arguments []; default message [name]]; default message [name is null]]
  </code></pre>
  
  <p>Here when customer.name is null, i'm getting response code as <strong>422</strong>(Unprocessable entity). But i want to return <strong>400</strong>(BAD Request). How can i override the response code here? Any document reference would be appreciated.</p>
  
  <p>note - i don't want to do these validation at controller's side, where can i can check and send the response code accordingly. 
  Like this one - <a href="https://stackoverflow.com/questions/52281666/how-to-return-400-http-status-code-with-notnull">How to return 400 http status code with @NotNull?</a></p>
  
  <p><strong>Update - issue was resolved -------</strong></p>
  
  <p>The issue is resolved for me. Thanks all for your response.</p>
  
  <p>Let me explain what was causing this,</p>
  
  <pre><code>HttpRequest ----&gt; @Valid on RequestBody ----&gt; Javax validation on Request object-----&gt; If any of the validation fails, **MethodArgumentNotValidException** exception is thrown.
  </code></pre>
  
  <p>It is the responsibility of the developer to catch this exception and throw corresponding http response code.
  In my case, the exception handler was already there and it was catching this <strong>MethodArgumentNotValidException</strong> and returning HttpStatus.UNPROCESSABLE_ENTITY. Thats the reason i was seeing 422 error code.</p>
  
  <p>Now i have changed the exception handler as below,</p>
  
  <pre><code>@ExceptionHandler(MethodArgumentNotValidException.class)
      public ResponseEntity&lt;ErrorResource&gt;
      handleMethodArgumentNotValidException(MethodArgumentNotValidException exception, HttpServletRequest request) {
  
          List&lt;FieldErrorResource&gt; fieldErrorResources = new ArrayList&lt;&gt;();
          BindingResult bindingResult = exception.getBindingResult();
          for (FieldError constraintViolation : bindingResult.getFieldErrors()) {
              fieldErrorResources.add(FieldErrorResource.builder()
                      .field(constraintViolation.getField())
                      .resource(request.getContextPath())
                      .message(constraintViolation.getDefaultMessage()).build());
          }
  
          return responseEntityFor(HttpStatus.BAD_REQUEST,
                  "The content you've sent contains " + bindingResult.getErrorCount() + " validation errors.", fieldErrorResources);
      }
  </code></pre>