title: Spring MVC method handling json and form params
body: <p>I want the handle the content types application/x-www-form-urlencoded and application/json in single spring mvc method. </p>
  
  <p>I've a requirement in rest service to accept input as form parameters or json. I can achieve this by writing two methods. Either form params or json, the response will be always json.  </p>
  
  <pre><code>@RequestMapping (method = RequestMethod.POST, produces = {"application/json"},
          consumes = {"application/x-www-form-urlencoded"})
  public @ResponseBody Book createBook(Book book)
          throws Exception {
      return book;
  }
  
  @RequestMapping (method = RequestMethod.POST, produces = {"application/json"},
          consumes = {"application/json"})
  public @ResponseBody Book createBookJSON(@RequestBody Book book)
          throws Exception {
      return book;
  } 
  </code></pre>
  
  <p>Is it possible to combine these two methods into one and make it work? Any help will be much appreciated. </p>
  
  <p><strong>Edit</strong></p>
  
  <p><em><strong>I've Implemented the same, my controllers and configuration are given below, but when I send json request I get null values as response.</em></strong> </p>
  
  <p>When I send the form params it is working fine. Help me to find out the issue. </p>
  
  <p><strong>Controller method</strong></p>
  
  <pre><code> @RequestMapping (method = RequestMethod.POST, produces = {"application/json", "application/xml"}, consumes = {"application/x-www-form-urlencoded", "application/json"})                   
      public @ResponseBody Book createBook(Book book)
              throws Exception {
          return book;
      }
  </code></pre>
  
  <p><strong>servlet-context</strong>
  </p>
  
  <pre><code>&lt;mvc:view-controller path="/" view-name="index"/&gt;
  
  &lt;context:annotation-config /&gt;
  
  &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/&gt;
  
  &lt;bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/&gt;
  
  &lt;bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&gt;
      &lt;property name="order" value="1" /&gt;
      &lt;property name="mediaTypes"&gt;
          &lt;map&gt;
              &lt;entry key="xml" value="application/xml" /&gt;
              &lt;entry key="json" value="application/json" /&gt;
          &lt;/map&gt;
      &lt;/property&gt;
      &lt;property name="defaultViews"&gt;
          &lt;list&gt;
              &lt;!-- JSON View --&gt;
              &lt;bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /&gt;
  
              &lt;!-- JAXB XML View --&gt;
              &lt;bean class="org.springframework.web.servlet.view.xml.MarshallingView"&gt;
                  &lt;constructor-arg&gt;
                      &lt;ref bean="jaxb2Marshaller" /&gt;
                  &lt;/constructor-arg&gt;
              &lt;/bean&gt;
  
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="ignoreAcceptHeader" value="true" /&gt;
  &lt;/bean&gt;
  
  &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" &gt;
      &lt;property name="order" value="1" /&gt;
      &lt;property name="messageConverters"&gt;
          &lt;list&gt;
              &lt;bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" &gt;
                  &lt;property name="supportedMediaTypes" value="application/json"/&gt;
              &lt;/bean&gt;
  
              &lt;bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"&gt;
                  &lt;property name="marshaller" ref="jaxb2Marshaller" /&gt;
                  &lt;property name="unmarshaller" ref="jaxb2Marshaller" /&gt;
                  &lt;property name="supportedMediaTypes" value="application/xml"/&gt;
              &lt;/bean&gt;
  
              &lt;bean class = "org.springframework.http.converter.FormHttpMessageConverter"&gt;
                  &lt;property name="supportedMediaTypes" value = "application/x-www-form-urlencoded" /&gt;
              &lt;/bean&gt;
  
              &lt;bean class = "org.springframework.http.converter.StringHttpMessageConverter"&gt;
                  &lt;property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" /&gt;
              &lt;/bean&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" &gt;
      &lt;property name="classesToBeBound"&gt;
          &lt;list&gt;
              &lt;value&gt;com.lt.domain.Book&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="order" value="2" /&gt;
      &lt;property name="prefix" value="/WEB-INF/views/"/&gt;
      &lt;property name="suffix" value=".jsp"/&gt;
  &lt;/bean&gt;
  </code></pre>