title: Spring Rest service @RequestBody nested json parsing failed
body: <ul>
  <li>Spring 4.3.2 </li>
  <li>Jackson 2.7.0</li>
  </ul>
  
  <p>I have created microservice using spring rest. I have a requirement that the RequestBody should have a nested json. </p>
  
  <p><strong>RequestWrapper.java:</strong></p>
  
  <pre><code>public class RequestWrapper implements Serializable{ 
  private String id;
  private String message; // json in string format
  /**
  getter &amp; setter
  **/
  }
  </code></pre>
  
  <p>The message attribute is dynamic, for instance, for one request it may have json of "Person" object, fo another request it may have different object.</p>
  
  <p>Everything worked well when I had the nested json as string and later converted to actual object
  Ex:</p>
  
  <pre><code>{
  "id":"sample",
  "message":"{\"age\":\"12\"}"
  }
  </code></pre>
  
  <p><strong>Controller Sample:</strong></p>
  
  <pre><code>@Controller
  @EnableWebMvc
  public class RequestController{
  @RequestMapping(value = "/request.htm", method = RequestMethod.POST, headers = "Accept=*/*", consumes = MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public ResponseWrapper handleRequest(@RequestBody RequestWrapper requestWrapper,
          HttpServletResponse response) {
  ResponseWrapper responseWrapper = new ResponseWrapper();
  ObjectMapper mapper = new ObjectMapper();
  Person person= mapper.readValue(requestWrapper.getMessage(), Person.class);
      }
  }
  </code></pre>
  
  <p>Junit test case were also successfully executed with this approach.</p>
  
  <p>Now I have modified the <strong>RequestWrapper.java</strong>, to have the type of message attribute as <strong>Object</strong> instead of <strong>String</strong></p>
  
  <pre><code>public class RequestWrapper&lt;Object extends Serializable&gt; implements Serializable{
  private String id;
      private Object message; // Changed to Object
      /**
      getter &amp; setter
      **/
      }
  </code></pre>
  
  <p>Changed the <strong>Controller class</strong> as below</p>
  
  <pre><code>@Controller
  @EnableWebMvc
  public class CipherController{
  @RequestMapping(value = "/request.htm", method = RequestMethod.POST, headers = "Accept=*/*", consumes = MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public ResponseWrapper handleRequest(@RequestBody RequestWrapper requestWrapper,
          HttpServletResponse response) {
  ResponseWrapper responseWrapper = new ResponseWrapper();
  ObjectMapper mapper = new ObjectMapper();
  Person person= (Person)requestWrapper.getMessage();
  }
  }
  </code></pre>
  
  <p>the new json format now looks like below:</p>
  
  <p>Ex:</p>
  
  <pre><code>{
  "id":"sample",
  "message":{"age":"12"}
  }
  </code></pre>
  
  <p>When I execute the Junit test case, I get below exception</p>
  
  <pre><code>WARNING: Failed to read HTTP message: 
  
  org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of java.io.Serializable, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
   at [Source: java.io.PushbackInputStream@3e58d65e; line: 1, column: 115] (through reference chain: com.common.model.RequestWrapper["message"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.io.Serializable, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
  </code></pre>
  
  <p>Jackson dependency in pom.xml</p>
  
  <pre><code>&lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
          &lt;version&gt;2.7.0&lt;/version&gt;
       &lt;/dependency&gt;
       &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
          &lt;version&gt;2.7.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
          &lt;version&gt;2.7.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-core-asl&lt;/artifactId&gt;
      &lt;version&gt;1.9.13&lt;/version&gt;
  &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-mapper-asl&lt;/artifactId&gt;
      &lt;version&gt;1.9.13&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>I assume that Jackson parser could not identify to which object instance the nested json(message) should be mapped to, but I do not know how to fix the problem.</p>