title: How to use javax.validation and JSON request in Spring 4 MVC?
body: <p>I'm developing a Web App using Spring 4 MVC. I want to know If I can <strong>validate</strong> JSON request objects with <strong>javax.validation API</strong>. For example I have this chunk of my <strong>entity</strong> code:</p>
  
  <pre><code>    ...       
      @JsonProperty("cheFecha")
      @NotNull
      @Column(name = "che_fecha")
      @Temporal(TemporalType.DATE)
      @DateTimeFormat(style = "M-")
      private Date SsiCheque.cheFecha;
  
      @JsonProperty("cheMonto")
      @NotNull
      @JsonSerialize(using = CurrencySerializer.class)
      @Column(name = "che_monto", precision = 10, scale = 2)
      private BigDecimal SsiCheque.cheMonto;
      ...
  </code></pre>
  
  <p>I have the <strong>controller</strong> code:</p>
  
  <pre><code>@RequestMapping(value = "/addCheck", method = RequestMethod.POST)
  public @ResponseBody SsiCheque addChecks(@Valid SsiCheque ssiCheque, BindingResult result) {
  
      //ssiCheque.persist();
      System.out.println("add" + result.getErrorCount());// Zero when there are errors
      return ssiCheque;
  }
  </code></pre>
  
  <p>And finally I have the jQuery code:</p>
  
  <pre><code>    var formData = $("#formAddChecks :input").serializeArray();
      $.ajax({
          type: "POST",
          url: "addCheck",
          data: formData,
          beforeSend: function ( xhr ) {
              console.log("before Send");
          },
          error: function (request, status, error) {            
              console.log('Error ' + "\n" + status + "\n" + error);
          },
          success: function(data) {
              console.log(data);
          }
      });
  </code></pre>
  
  <p>The JSON object is arriving correctly to the controller but I want to validate the JSON with the entity javax.annotations API. What I have seen is only using <strong>custom validators</strong> and "rewrite" the validation code. </p>
  
  <p>Is this the only way to validate JSON?</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>I followed the @James Massey suggestions and my code looks like this right now:</p>
  
  <p>Controller</p>
  
  <pre><code>@RequestMapping(value = "/addCheck", method = RequestMethod.POST)
  @ResponseBody
  public SsiCheque addChecks(@Valid @RequestBody SsiCheque ssiCheque, BindingResult result) {
  
      //ssiCheque.persist();
      System.out.println("agregar " + result.getErrorCount());
      return ssiCheque;
  }
  </code></pre>
  
  <p>Javascript file</p>
  
  <pre><code>    var ssiCheque = {
              cheNumero : $("#formAddChecks cheNumero").val(),
              cheRecepto : $("#formAddChecks cheReceptor").val(),
              cheMonto : $("#formAddChecks cheMonto").val(),
              cheFecha : $("#formAddChecks cheFecha").val(),
              cheConcepto : $("#formAddChecks cheConcepto").val()
      };
  
  
      $.ajax({
          type: "POST",
          contentType: "application/json",
          url: "addCheck",
          data: ssiCheque,
          dataType: "json",
          beforeSend: function ( xhr ) {
              console.log("before Send");
          },
          error: function (request, status, error) {            
              console.log('Error ' /*+ request.responseText*/ + "\n" + status + "\n" + error);
          },
          success: function(data) {
              console.log(data);
          }
      });
  </code></pre>
  
  <p>But I'm getting an 400 Error (Incorrect request) when I submit the form and execute the <strong>Ajax function</strong>. I have faced this error before when the json object format and the controller specs were incompatible, but in this time I don't know why can be the error.</p>
  
  <p>Thanks again!</p>