title: Cannot bind automatically in AJAX post request to a Java object in Spring MVC
body: <p>I have two issues:</p>
  
  <ol>
  <li>Cannot get my parameters automatically bound into an Java object (they are being
  posted with jQuery using AJAX)</li>
  <li>I am getting a 406 error, I think this means I am not being able to return the JSON object I would like to</li>
  </ol>
  
  <p>I believe the two problem are different, but the solution to both rely in the same part of the code. Could you help me please? Thanks a lot in advance!</p>
  
  <p><strong>Ajax Request</strong></p>
  
  <p>I am trying to post a very simple object, having an email and a name, like this.</p>
  
  <pre><code>var dataObject = {
      name : this.userName,
      email : this.userEmail,
  };
  this.$.ajax({
      type : "POST",
      url : '/stock-events/register-to-stock',
      contentType: "application/json",
      data : JSON.stringify(dataObject),
      success : function(data) {
          console.info("data");
      },
      dataType : 'json'
  });
  </code></pre>
  
  <p><strong>Spring MCV Controller</strong></p>
  
  <pre><code>    @ResponseBody @RequestMapping(value = "/register-to-stock", method = RequestMethod.POST)
  public StockAlarmJsonResponse submitForm(@Valid @RequestBody StockAlarmModel stockAlarm,
          BindingResult result, Model m) {
      System.out.println("hola");
      System.out.println(stockAlarm);
      StockAlarmJsonResponse response = new StockAlarmJsonResponse();
      if (result.hasErrors()) {
          System.out.println(result);
          response.fail();
      }else{
          System.out.println(stockAlarm);
          response.success();
      }
      return response;
  }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>  public class StockAlarmModel {
  
  
  @NotNull @Max(100) @Email
  private String email;
  
  @Max(100)
  private String name;
  
  @Override
  public String toString() {
      return "Email: " + email + '\n' + "Name: " + name;
  }
  }
  </code></pre>
  
  <p><strong>Error I get in Java (looks like the parameters are not there)</strong></p>
  
  <pre><code> org.springframework.validation.BeanPropertyBindingResult: 1 errors
   Field error in object 'stockAlarmModel' on field 'email': rejected value [null]; codes   [NotNull.stockAlarmModel.email,NotNull.email,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [stockAlarmModel.email,email]; arguments []; default message [email]]; default message [may not be null]
  </code></pre>
  
  <p><strong>Error I get in chrome bug (looks that json object is not being returned successfully)</strong></p>
  
  <pre><code>  POST http://localhost:8080/stock-events/register-to-stock 400 (Bad Request) jquery-     1.9.1.js:8526
    send jquery-1.9.1.js:8526
    jQuery.extend.ajax jquery-1.9.1.js:7978
    StockAlarm.registerAlarm StockAlarm.js:131
    (anonymous function) StockAlarm.js:87
   jQuery.event.dispatch jquery-1.9.1.js:3074
  elemData.handle
  </code></pre>
  
  <p><strong>What Chrome Bug Shows for Request</strong></p>
  
  <p>Content Type / Application Json</p>
  
  <p>Request Payload: </p>
  
  <pre><code>{"name":"santiago","email":"santiago@tiendanube.com"}
  </code></pre>