title: @RequestBody Sent Through Spring MVC JSON Jackson Processor
body: <p>I have read some many similiar problems inlcluding: <a href="https://stackoverflow.com/questions/5908466/jquery-spring-mvc-requestbody-and-json-making-it-work-together">JQuery, Spring MVC @RequestBody and JSON - making it work together</a> <a href="https://stackoverflow.com/questions/10253209/json-request-with-jquery-ajax-with-spring">JSON request with JQuery/Ajax with Spring</a></p>
  
  <p>The requirement is that the server will only accept application/json types. I am using a Spring MVC Controller. The code sends a response back as JSON through @ResponseBody. I want to get information through the @RequestBody in my Spring MVC Controller. I am using JSP to send JSON to Spring MVC Controller. My code and Spring MVC can be seen below:</p>
  
  <p>I am new to JSON and Javascript.</p>
  
  <p>JSP - index.jsp</p>
  
  <pre><code>&lt;%@page language="java" contentType="text/html"%&gt; 
  &lt;html&gt; 
  &lt;head&gt; 
  &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"&gt;&lt;/script&gt;   
  &lt;script type="text/javascript"&gt; 
  $('#myForm').on('submit', function(e) {  
  var frm = $("#myForm"); 
  var dat = JSON.stringify(frm.serializeArray());  
  
   $.ajax({  
   type: 'POST',  
   url: $('#myForm').attr('action'),  
   data: dat, 
   contentType: 'application/json', 
   dataType: 'json', 
   error: function() { 
      alert('failure'); 
   } 
   success: function(hxr) {  
       alert("Success: " + xhr);  
   } 
    });  
  );  
     };  
  &lt;/script&gt;  
  &lt;/head&gt; 
  &lt;body&gt; 
  &lt;h2&gt;Application&lt;/h2&gt; 
  &lt;form id="myForm" action="/application/save" method="POST" accept="application/json" onclick="i()"&gt; 
  &lt;input type="text" name="userId" value="User"&gt; 
  &lt;input type="submit" value="Submit"&gt; 
  &lt;/form&gt; 
  &lt;/body&gt; 
  &lt;/html&gt; 
  </code></pre>
  
  <p>When running this I am not getting any output. In the Chrome I get 404 Not found error and in Tomcat I get the following error:</p>
  
  <pre><code>org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver                        handleNoSuchRequestHandlingMethod 
  WARNING: No matching handler method found for servlet request: path '/application/sa 
   ve', method 'POST', parameters map['userId' -&gt; array&lt;String&gt;['User']] 
  </code></pre>
  
  <p>Is something wrong here in the JSP part?</p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
  &lt;web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" 
   version="2.5"&gt; 
  
   &lt;display-name&gt;WebApp&lt;/display-name&gt; 
  
   &lt;context-param&gt; 
      &lt;!-- Specifies the list of Spring Configuration files in comma separated format.--&gt; 
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; 
      &lt;param-value&gt;/WEB-INF/spring/service.xml&lt;/param-value&gt; 
   &lt;/context-param&gt; 
  
   &lt;listener&gt; 
      &lt;!-- Loads your Configuration Files--&gt; 
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; 
   &lt;/listener&gt; 
  
   &lt;servlet&gt; 
      &lt;servlet-name&gt;application&lt;/servlet-name&gt; 
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; 
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; 
   &lt;/servlet&gt; 
  
   &lt;servlet-mapping&gt; 
      &lt;servlet-name&gt;application&lt;/servlet-name&gt; 
      &lt;url-pattern&gt;/&lt;/url-pattern&gt; 
   &lt;/servlet-mapping&gt; 
  
   &lt;welcome-file-list&gt; 
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt; 
   &lt;/welcome-file-list&gt;     
  &lt;/web-app&gt; 
  </code></pre>
  
  <p>service.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; 
  &lt;beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:util="http://www.springframework.org/schema/util" 
  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"&gt; 
  
  &lt;context:component-scan base-package="com.web"/&gt; 
  
  &lt;mvc:annotation-driven/&gt; 
  
  &lt;context:annotation-config/&gt; 
  
  &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/&gt; 
  
  &lt;bean id="jacksonMessageChanger" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt; 
      &lt;property name="supportedMediaTypes" value="application/json"/&gt; 
  &lt;/bean&gt; 
  
  &lt;!-- &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt; 
      &lt;property name="messageConverters"&gt; 
          &lt;list&gt; 
              &lt;ref bean="jacksonMessageChanger"/&gt; 
          &lt;/list&gt; 
      &lt;/property&gt; 
  &lt;/bean&gt;--&gt; 
  
  &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt; 
      &lt;property name="messageConverters"&gt; 
          &lt;util:list id="beanList"&gt; 
              &lt;ref bean="jacksonMessageChanger"/&gt; 
          &lt;/util:list&gt; 
      &lt;/property&gt; 
  &lt;/bean&gt; 
  
  &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; 
      &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt; 
      &lt;property name="suffix" value=".jsp"/&gt; 
  &lt;/bean&gt; 
  
  &lt;!-- &lt;bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&gt; 
      &lt;property name="mediaTypes"&gt; 
          &lt;map&gt; 
              &lt;entry key="json" value="application/json"/&gt; 
          &lt;/map&gt; 
      &lt;/property&gt; 
  &lt;/bean&gt;--&gt;   
  </code></pre>
  
  <p> </p>
  
  <p>Controller</p>
  
  <pre><code>package com.web; 
  
  import org.springframework.stereotype.Controller; 
  import org.springframework.web.bind.annotation.RequestMapping; 
  import org.springframework.web.bind.annotation.RequestMethod; 
  import org.springframework.web.bind.annotation.ResponseBody; 
  import org.springframework.web.bind.annotation.RequestBody; 
  import com.webchannel.domain.User; 
  import com.webchannel.domain.UserResponse; 
  
  @Controller 
  @RequestMapping("/application/*") 
  public class SaveController { 
  
   @RequestMapping(value = "save", method = RequestMethod.POST, headers = {"content-type=application/json"}) 
   public @ResponseBody UserResponse save(@RequestBody User user) throws Exception { 
    UserResponse userResponse = new UserResponse(); 
   System.out.println("UserId :" + " " + user.getUserId()); 
   return userResponse; 
  } 
  
  @RequestMapping(value = "delete", method = RequestMethod.GET) 
  public @ResponseBody UserResponse delete() { 
     System.out.println("Delete"); 
     UserResponse userResponse = new UserResponse(); 
     userResponse.setSuccess(true); 
     userResponse.setVersionNumber("1.0"); 
     return userResponse; 
  </code></pre>
  
  <p>} 
    } </p>
  
  <p>When invoking /application/delete I get JSON returned. So I know my JacksonProcessor is configured correctly. The problem is in @RequestBody.</p>
  
  <p>Where am I going wrong?</p>
  
  <p>If I remove the headers in the code below I get a 415 error.</p>
  
  <pre><code>@RequestMapping(value = "save", method = RequestMethod.POST) 
   public @ResponseBody UserResponse save(@RequestBody User user) throws Exception { 
    UserResponse userResponse = new UserResponse(); 
   System.out.println("UserId :" + " " + user.getUserId()); 
   return userResponse; 
  } 
  </code></pre>
  
  <p>I am almost close but help would be appreciated.</p>