How to pass JSON as @RequestBody in Spring MCV

I would like to have a bidirectional JSON to Java serialization

I"m using successfully the Java to JSON to JQuery path... (@ResponseBody) e.g.

@RequestMapping(value={"/fooBar/{id}"}, method=RequestMethod.GET)

     public @ResponseBody FooBar getFooBar(

            @PathVariable String id,

            HttpServletResponse response , ModelMap model) {




and In JQuery I use

$.getJSON("fooBar/1", function(data) {

    //do something


this works well (e.g. annotations work already, thanks to all the answerers)

However, how do I do the reverse path: have JSON be serialized to a Java Object back using RequestBody?

no matter what I try, I can"t get something like this to work:

@RequestMapping(value={"/fooBar/save"}, method=RequestMethod.POST)

public String saveFooBar(@RequestBody FooBar fooBar,

        HttpServletResponse response , ModelMap model) {

  //This method is never called. (it does when I remove the RequestBody...)


I have Jackson configured correctly (it serializes on the way out) and I have MVC set as annotations driven of course

How do I make it work? is it possible at all? or is Spring / JSON / JQuery is oneway (out)?


I'm pretty sure you only have to register MappingJacksonHttpMessageConverter

(the easiest way to do that is through <mvc:annotation-driven /> in XML or @EnableWebMvc in Java)


Here's a working example:

Maven POM

<project xmlns="" xmlns:xsi=""     xsi:schemaLocation="">     <modelVersion>4.0.0</modelVersion><groupId>test</groupId><artifactId>json</artifactId><packaging>war</packaging>     <version>0.0.1-SNAPSHOT</version><name>json test</name>     <dependencies>         <dependency><!-- spring mvc -->             <groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>3.0.5.RELEASE</version>         </dependency>         <dependency><!-- jackson -->             <groupId>org.codehaus.jackson</groupId><artifactId>jackson-mapper-asl</artifactId><version>1.4.2</version>         </dependency>     </dependencies>     <build><plugins>             <!-- javac --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId>             <version>2.3.2</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin>             <!-- jetty --><plugin><groupId>org.mortbay.jetty</groupId><artifactId>jetty-maven-plugin</artifactId>             <version>7.4.0.v20110414</version></plugin>     </plugins></build> </project>

in folder src/main/webapp/WEB-INF


<web-app xmlns="" xmlns:xsi=""     xsi:schemaLocation=""     version="2.4">     <servlet><servlet-name>json</servlet-name>         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>         <load-on-startup>1</load-on-startup>     </servlet>     <servlet-mapping>         <servlet-name>json</servlet-name>         <url-pattern>/*</url-pattern>     </servlet-mapping> </web-app>


<beans xmlns=""     xmlns:xsi=""     xsi:schemaLocation="               ">      <import resource="classpath:mvc-context.xml" />  </beans>

in folder src/main/resources:


<beans xmlns=""     xmlns:mvc="" xmlns:xsi=""     xmlns:context=""     xsi:schemaLocation="">      <mvc:annotation-driven />     <context:component-scan base-package="test.json" /> </beans>

In folder src/main/java/test/json

@Controller @RequestMapping("/test") public class TestController {      @RequestMapping(method = RequestMethod.POST, value = "math")     @ResponseBody     public Result math(@RequestBody final Request request) {         final Result result = new Result();         result.setAddition(request.getLeft() + request.getRight());         result.setSubtraction(request.getLeft() - request.getRight());         result.setMultiplication(request.getLeft() * request.getRight());         return result;     }  }

public class Request implements Serializable {     private static final long serialVersionUID = 1513207428686438208L;     private int left;     private int right;     public int getLeft() {return left;}     public void setLeft(int left) {this.left = left;}     public int getRight() {return right;}     public void setRight(int right) {this.right = right;} }

public class Result implements Serializable {     private static final long serialVersionUID = -5054749880960511861L;     private int addition;     private int subtraction;     private int multiplication;      public int getAddition() { return addition; }     public void setAddition(int addition) { this.addition = addition; }     public int getSubtraction() { return subtraction; }     public void setSubtraction(int subtraction) { this.subtraction = subtraction; }     public int getMultiplication() { return multiplication; }     public void setMultiplication(int multiplication) { this.multiplication = multiplication; } }

You can test this setup by executing mvn jetty:run on the command line, and then sending a POST request:

URL:        http://localhost:8080/test/math mime type:  application/json post body:  { "left": 13 , "right" : 7 }

I used the Poster Firefox plugin to do this.

Here's what the response looks like:

Answer is