jQuery and Spring MVC
<p>Actually I am trying to send data from jQuery to Spring controller and in Spring controller I am trying to get that data in a custom bean using <code>@RequestBody</code>. But I am unable to do so. It's not working. But when I am sending the same data as <code>@RequestParam</code> it's working fine, but in case of <code>@RequestBody</code> nothing is happened. I have serialized the html form and posting that data to the controller. I am pasting all the codes. I am using jQuery version <code>jquery-1.6.2.min.js</code> and below is the list of the jars I am using :</p>

<pre><code>commons-fileupload-1.1.1.jar
commons-io-1.2.jar
commons-logging-1.1.1.jar
hibernate-validator-4.0.2.GA.jar
jackson-all-1.7.6.jar
jackson-mapper-asl-1.5.2.jar
jstl-1.2.jar
log4j-1.2.14.jar
servlet-2.3.jar
slf4j-api-1.5.6.jar
slf4j-log4j12-1.5.6.jar
spring-asm-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring-tx-2.5.5.jar
spring-web-3.0.5.RELEASE.jar
spring-webmvc-3.0.5.RELEASE.jar
validation-api-1.0.0.GA.jar
</code></pre>

<p>Below is the controller code :</p>

<pre><code>package com.devmanuals.tutorial.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.devmanuals.tutorial.service.ArithmeticService;

/**
 * Handles and retrieves the main requests
 */
@Controller
@RequestMapping("/main/ajax")
public class AjaxController {

    protected static Logger logger = Logger.getLogger("controller");

    @Resource(name="springService")
    private ArithmeticService springService;

    /**
     * Handles and retrieves the AJAX Add page
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String getAjaxAddPage() {

        System.out.println("Received request to show AJAX, add page");
        logger.debug("Received request to show AJAX, add page");

        // It resolves the /WEB-INF/jsp/ajax-add-page.jsp
        return "ajax-add-page";
    }

    /**
     * Handles request for adding two numbers
     */
    /**
     * Handles request for adding two numbers
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody Integer create(@RequestParam(value="inputNumber1", required=true) Integer inputNumber1,
                                @RequestParam(value="inputNumber2", required=true) Integer inputNumber2,
                                Model model) {
        logger.debug("Received request to add two numbers");
        Integer sum = springService.add(inputNumber1, inputNumber2);
        logger.debug("Result is ........" +sum);
        return sum;
    }


    @RequestMapping(method=RequestMethod.POST, value = "/bean")
        public @ResponseBody Integer create(@RequestBody NumberBean account, HttpServletResponse response,ModelMap model) {

        System.out.println("Test...........");

            if (account==null) {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return 1;
            } else {
                return 20;
            }
        }


    /**
     * Handles request for adding two numbers
     */
    /* @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody Integer add(@RequestBody NumberBean number,
                                Model model) {
        //logger.debug("Received request to add two numbers");

        System.out.println(number);

        Integer sum = springService.add(number.getInputNumber1(), number.getInputNumber2());
        return sum;
    }*/
}
</code></pre>

<p>And the jsp is as below:</p>

<pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;

&lt;script type="text/javascript"
    src="/SpringjQueryTest/jquery/jquery-1.6.2.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
    var jq = jQuery.noConflict();
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;h3&gt;Enter Two Numbers for Addition:&lt;/h3&gt;
&lt;form id="test"&gt;
&lt;table border="1" height="10%" width="10%"&gt;
&lt;tr&gt;&lt;td&gt;&lt;table border="0" height="100%" width="100%"&gt;

    &lt;tr&gt;
        &lt;td&gt;&lt;input id="x" type="text" size="5" name="inputNumber1"&gt;&lt;/td&gt;
        &lt;td&gt;+&lt;/td&gt;
        &lt;td&gt;&lt;input id="y" type="text" size="5" name="inputNumber2"&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td colspan="3" align="center"&gt;&lt;input type="submit" value=" Submit "
            onclick=add(); /&gt;&lt;/td&gt;
    &lt;/tr&gt;

&lt;/table&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/form&gt;

&lt;script type="text/javascript"&gt;
    function add() {
        jq(function() {

            var number = jq('form').serialize();
            alert(number);

            jq.post("/SpringjQueryTest/dev/main/ajax/bean", number,

            function(data) {
                alert("Added Number below the button");
                alert(data);

            });
        });
    }
&lt;/script&gt;
&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>And here is the applicationContext.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: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"&gt;

    &lt;!-- Activates various annotations to be detected in bean classes --&gt;
    &lt;context:annotation-config /&gt;

    &lt;!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
     For example @Controller and @Service. Make sure to set the correct base-package--&gt;
    &lt;context:component-scan base-package="com.devmanuals.tutorial" /&gt;

    &lt;!-- Configures the annotation-driven Spring MVC Controller programming model.
    Note that, with Spring 3.0, this tag works in Servlet MVC only!  --&gt;
    &lt;mvc:annotation-driven /&gt; 

&lt;/beans&gt;
</code></pre>

<p>And here is spring-servlet.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:tx="http://www.springframework.org/schema/tx"
    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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;


&lt;bean id="jacksonMessageConverter"
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt;&lt;/bean&gt;


    &lt;bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
        &lt;property name="messageConverters"&gt;
            &lt;list&gt;
                &lt;ref bean="jacksonMessageConverter" /&gt;
            &lt;/list&gt;
        &lt;/property&gt;
    &lt;/bean&gt;     

    &lt;!-- Declare a view resolver --&gt;
 &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
        &lt;property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" /&gt;
        &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
        &lt;property name="suffix" value=".jsp" /&gt;
    &lt;/bean&gt;
&lt;/beans&gt;
</code></pre>

<p>Here is the NumberBean.java</p>

<pre><code>package com.devmanuals.tutorial.controller;

import java.io.Serializable;

public class NumberBean implements Serializable{

    private int inputNumber1;
    private int inputNumber2;

    public int getInputNumber1() {
        return inputNumber1;
    }
    public void setInputNumber1(int inputNumber1) {
        this.inputNumber1 = inputNumber1;
    }
    public int getInputNumber2() {
        return inputNumber2;
    }
    public void setInputNumber2(int inputNumber2) {
        this.inputNumber2 = inputNumber2;
    }
    @Override
    public String toString() {
        return "NumberBean [inputNumber1=" + inputNumber1 + ", inputNumber2="
                + inputNumber2 + "]";
    }




}
</code></pre>

<p>And here is the ArithmeticService.java</p>

<pre><code>package com.devmanuals.tutorial.service;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Service enables the class to be used as a Spring service
 * @Transactional enables transaction support for this class
 */
@Service("springService")
@Transactional
public class ArithmeticService {

    protected static Logger logger = Logger.getLogger("service");

    /**
     * Adds two numbers
     */
    public Integer add(Integer operand1, Integer operand2) {
        logger.debug("Adding two numbers");
        return operand1 + operand2;
    }

}
</code></pre>

<p>Here is my web.xml :</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app id="WebApp_ID" version="2.4" 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_4.xsd"&gt;

    &lt;servlet&gt;
        &lt;servlet-name&gt;spring&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;spring&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/dev/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;

    &lt;listener&gt;
        &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
    &lt;/listener&gt;

    &lt;welcome-file-list&gt;
        &lt;welcome-file&gt;/WEB-INF/jsp/ajax-add-page.jsp&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
&lt;/web-app&gt;
</code></pre>

<p><strong>UPDATE :</strong></p>

<p>as per the answers I am converting the serialized form value into json and in controller end I am getting it as a java bean through <code>@RequestBody</code>. But now the problem is the <code>@ResponseBody</code> is not working. Within the <code>jq.ajax</code> function the success callback method is not getting invoked. I am totally confused,as it was successfully working before I tried <code>@RequestBody</code>,with <code>@RequestParam</code>. Plz help me.......</p>

<p>the jQuery function :</p>

<pre><code>jq.ajax( {
                url : "/SpringjQueryTest/dev/main/ajax/test",
                type : "POST",
                data : json, 
                dataType : "json",
                contentType : "application/json; charset=utf-8",
                success : function(data) {
                    alert("Added Number below the button");
                    alert(data);
                },
            error : function(xhr, desc, err) {
                alert("Desc: " + desc + "\nErr:" + err);
            }
            });
</code></pre>

<p><strong>Updated controller method :</strong> </p>

<pre><code>/**
     * Handles request for adding two numbers
     */
     @RequestMapping(value = "/test", method = RequestMethod.POST)
    public @ResponseBody 
    Integer test(@RequestBody NumberBean number,HttpServletResponse response ) {
        logger.debug("Received request to add two numbers");
        response.setContentType("application/json");
        System.out.println(number);

        Integer sum = springService.add(number.getInputNumber1(), number.getInputNumber2());
        return sum;
    }
</code></pre>

<p>I am able to see the output </p>

<blockquote>
  <p><strong>Received request to add two numbers</strong>  </p>
</blockquote>

<p>in my console and even the sum is getting generated. But the success function of <code>jq.ajax</code> </p>

<pre><code>success : function(data) {
                    alert("Added Number below the button");
                    alert(data);
                                   }
</code></pre>

<p>is not getting invoked. Nothing is happened. Even no error messages. </p>

<p>Also I noticed in the log of <code>HttpTrace tool</code> that though I am explicitly setting the response content type as <code>application/json</code> in the controller method but the content type remains <code>text/html</code> only. </p>

<p>I am just wondering as it was successfully executing when I was using <code>@RequestParam</code>, but with <code>@RequestBody</code> is not working. </p>

<p>Please find the applicationContext.xml also</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: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"&gt;

    &lt;!-- Activates various annotations to be detected in bean classes --&gt;
    &lt;context:annotation-config /&gt;

    &lt;!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
     For example @Controller and @Service. Make sure to set the correct base-package--&gt;
    &lt;context:component-scan base-package="com.devmanuals.tutorial" /&gt;

    &lt;!-- Configures the annotation-driven Spring MVC Controller programming model.
    Note that, with Spring 3.0, this tag works in Servlet MVC only!  --&gt;
    &lt;mvc:annotation-driven /&gt; 

    &lt;bean id="jacksonMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt;&lt;/bean&gt;


    &lt;bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
        &lt;property name="messageConverters"&gt;
            &lt;list&gt;
                &lt;ref bean="jacksonMessageConverter" /&gt;
            &lt;/list&gt;
        &lt;/property&gt;
    &lt;/bean&gt;

    &lt;bean id="exceptionMessageAdapter"
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver"&gt;
        &lt;property name="messageConverters"&gt;
            &lt;list&gt;
                &lt;!-- Support JSON --&gt;
                &lt;ref bean="jacksonMessageConverter" /&gt;  
            &lt;/list&gt;
        &lt;/property&gt;
    &lt;/bean&gt;

&lt;/beans&gt;
</code></pre>

<p>And I do have <strong><code>jackson-all-1.8.3.jar</code></strong> in my classpath even. I am really confused.</p>
