title: How to automatically populate fields from ajax request in spring boot?
body: <p>I made an ajax request to the server for checking whether a particular username exists in the database. Here is the code for that..</p>
  
  <pre><code>$("input[placeholder='Username']").focusout(function() {
          var username = $("input[placeholder='Username']").val();
          if(username.length &lt;3 || username.length&gt;20) {
              $("#username_taken").html("Username must be between 3-20 characters!");
              usernameFlag = true;
          }
          else if(username != null) {
              $.ajax({
                  type: "POST",
                  url: "/data/validate_username",
                  data: username,
                  success: function(isValid) {
                      if(!isValid) {
                          $("#username_taken").html("The username has already been taken!");
                          usernameFlag = true;
                      }
                  },
                  error: function() {
                      $("#username_taken").html("Could not verify username from server!");
                  }
              });
          }
      });
  </code></pre>
  
  <p>In the server side I have written a RestController to handle this request and provide the data on whether the user exists or not.</p>
  
  <pre><code>// necessary imports done...
  
  @RequestMapping("/data")
  @RestController
  public class UserAccountRest {
  
      @Autowired
      private UserAccountService userAccountService;
  
      @PostMapping("/validate_username")
      public boolean validateUsername(String username) {
          return !userAccountService.accountExists(new UserAccount(username));
      }
  }
  </code></pre>
  
  <p>Normally spring auto populates the parameters like username, if it was a form submit. But here status 500 Internal Server Error occurs, and in the console it says that the id must not be null. This means that the username is not populated.</p>
  
  <p>I could probably use HttpRequest object in the parameter and get the username from it. But is there any way to configure so that the username is directly populated?</p>