title: How to send form data using Ajax+JQuery in form of JSON object to spring restful web services API?
body: <p>I am try to send JSON object data using Ajax+JQuery to send the <strong>Spring Restful web services</strong>, But It is not sending the data on eclipse console I am getting following message in eclipse console. </p>
  
  <p><strong>Message in eclipse:</strong></p>
  
  <pre><code> 16:47:36.293 [http-bio-8080-exec-10] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing POST request for [/HomeServiceProvider/customer/saveCustomer]
      16:47:36.293 [http-bio-8080-exec-10] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /customer/saveCustomer
      16:47:36.295 [http-bio-8080-exec-10] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
      16:47:36.295 [http-bio-8080-exec-10] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
      16:47:36.295 [http-bio-8080-exec-10] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
      16:47:36.295 [http-bio-8080-exec-10] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
      16:47:36.295 [http-bio-8080-exec-10] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
  </code></pre>
  
  <p><strong>My form.html file</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="ISO-8859-1"/&gt;
  &lt;meta header="application/json"&gt;
  &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  $(document).ready(function(){
    $("#button").click(function(){
    alert("1");
    var customer = new Object();
      customer.custName = $('#custName').val();
      customer.custMobile = $('#custMobile').val();
      customer.custEmail = $('#custEmail').val();
      customer.custAddress = $('#custAddress').val();
      alert("2"); 
                   $.ajax({
                       url: 'http://localhost:8080/HomeServiceProvider/customer/saveCustomer',
                       type: 'POST',
                       dataType: 'json',
                       data: customer,
                       success: function (data, textStatus, xhr) {
                           console.log(data);
                       },
                       error: function (xhr, textStatus, errorThrown) {
                           console.log('Error in Operation');
                       }
                   });
                   alert("3");
    });
    alert("3");
  });
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form method="POST"&gt;
      &lt;fieldset style="text-align:right; width:300px"&gt;
      &lt;legend&gt;&lt;b&gt;Registration Form&lt;/b&gt;&lt;/legend&gt;
          Name &lt;input type="text" id="custName" name="custName"/&gt;&lt;br/&gt;
          Mobile No &lt;input type="text" id="custMobile" name="custMobile"/&gt;&lt;br/&gt;
          Email &lt;input type="text" id="custEmail" name="custEmail"/&gt;&lt;br/&gt;
          Address &lt;input type="text" id="custAddress" name="custAddress"/&gt;&lt;br/&gt;       
          &lt;button id="button"&gt;Save Data&lt;/button&gt;
      &lt;/fieldset&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>CustomerRestController.java</strong></p>
  
  <pre><code> @RestController
      @RequestMapping("/customer")
      public class CustomerRestController {
  
          private static Logger log = LogManager.getLogger(CustomerRestController.class);
  
          @Value("${msg.customeradded}")
          private String message;
          @Value("${msg.successcode}")
          private int code;
  
          @Autowired
          private CustomerService customerService;
  
          @RequestMapping(value = "/saveCustomer", method = RequestMethod.POST, consumes = "application/json") 
          public @ResponseBody Status saveCustomer(@RequestBody String jsonString){
          //check whether u r receiving some data over here 
          System.out.println("received :" + jsonString);
              try { 
                 // customerService.saveCustomer(customer); 
                  return new Status(code, message);
              } catch (Exception e) {
                  return new Status(0, e.toString());
              } 
          } 
  
          @RequestMapping(value="/getAllCustomer",method=RequestMethod.GET, headers="Accept=application/json")
          public @ResponseBody List&lt;Customer&gt; getAllCustomer(){
              List&lt;Customer&gt; customers = null;
              try {
                  customers = customerService.getAllCustomer();
              log.info("Size:"+customers.size());
              log.info("customers:"+customers);
              } catch(Exception e) {
                  e.printStackTrace();
              }
              return customers;
          }
      }
  </code></pre>
  
  <p><strong>Customer.Java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "customer", catalog = "service4homes")
  public class Customer implements java.io.Serializable {
  
      private Integer CId;
      private String custName;
      private String custMobile;
      private String custEmail;
      private String custAddress;
  
      public Customer() {
      }
  
      public Customer(String custName, String custMobile, String custAddress) {
          this.custName = custName;
          this.custMobile = custMobile;
          this.custAddress = custAddress;
      }
  
      public Customer(String custName, String custMobile, String custEmail,
              String custAddress) {
          this.custName = custName;
          this.custMobile = custMobile;
          this.custEmail = custEmail;
          this.custAddress = custAddress;
      }
  
      @Id
      @GeneratedValue(strategy = IDENTITY)
      @Column(name = "c_id", unique = true, nullable = false)
      public Integer getCId() {
          return this.CId;
      }
  
      public void setCId(Integer CId) {
          this.CId = CId;
      }
  
      @Column(name = "cust_name", nullable = false, length = 50)
      public String getCustName() {
          return this.custName;
      }
  
      public void setCustName(String custName) {
          this.custName = custName;
      }
  
      @Column(name = "cust_mobile", nullable = false, length = 13)
      public String getCustMobile() {
          return this.custMobile;
      }
  
      public void setCustMobile(String custMobile) {
          this.custMobile = custMobile;
      }
  
      @Column(name = "cust_email", length = 100)
      public String getCustEmail() {
          return this.custEmail;
      }
  
      public void setCustEmail(String custEmail) {
          this.custEmail = custEmail;
      }
  
      @Column(name = "cust_address", nullable = false, length = 300)
      public String getCustAddress() {
          return this.custAddress;
      }
  
      public void setCustAddress(String custAddress) {
          this.custAddress = custAddress;
      }
  
  }
  </code></pre>