title: Automatic conversion of JSON form parameter in Spring MVC 4.0
body: <p>I am trying to build a Spring MVC controller which will receive a POSTed form with a parameter in JSON format, and have Spring automatically convert it to a Java object.</p>
  
  <ul>
  <li>Request content type is <code>application/x-www-form-urlencoded</code></li>
  <li>The name of the parameter that contains a JSON string is <code>data.json</code></li>
  </ul>
  
  <p>This is the controller:</p>
  
  <pre><code>@Controller
  public class MyController {
      @RequestMapping(value = "/formHandler", method = RequestMethod.POST)
      public @ResponseBody String handleSubscription(
          @RequestParam("data.json") MyMessage msg) {
          logger.debug("id: " + msg.getId());
          return "OK";
      }
  }
  </code></pre>
  
  <p>And this is what the MyMessage object looks like:</p>
  
  <pre><code>public class MyMessage {
      private String id;
      // Getter/setter omitted for brevity
  }
  </code></pre>
  
  <p>Perhaps not surprisingly, posting a form with parameter data.json={"id":"Hello"} results in HTTP error 500 with this exception:</p>
  
  <pre><code>org.springframework.beans.ConversionNotSupportedException:
      Failed to convert value of type 'java.lang.String' to required type 'MyMessage' 
  nested exception is java.lang.IllegalStateException:
      Cannot convert value of type [java.lang.String] to required type [MyMessage]: no matching editors or conversion strategy found
  </code></pre>
  
  <p>If I read the <a href="http://docs.spring.io/spring/docs/4.0.x/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html" rel="noreferrer">MappingJackson2HttpMessageConverter docs</a> correctly, Jackson JSON conversion is triggered by Content-Type <code>application/json</code>, which I obviously cannot use since this is a form POST (and I don't control the POSTing part).</p>
  
  <p>Is it possible to get Spring to convert the JSON string into an instance of MyMessage, or should I just give up, read it as a String and perform the conversion myself?</p>