title: Accept arbitrary JSON as RequestBody in Spring
body: <p>As the title suggests, I'd like my controller class to accept (mostly) arbitrary JSON in the RequestBody as part of a POST. Before you ask why I'd want unworkable JSON, it's because the JSON is simply passing through to a storage backend with very minimal processing (only extracting one field).</p>
  
  <p>There are three required fields in this JSON: <code>data</code>, <code>schema</code>, and <code>resourceID</code>. Here's what my request class looks like:</p>
  
  <pre><code>public class MyRequestBody implements Serializable {
  
      private JsonObject data;
      private JsonObject schema;
      private String resourceID;
      ...
  </code></pre>
  
  <p>I already understand the serialization issues that cause this not to work. The reason I don't want to expand this data model further to define fields for <code>data</code> and <code>schema</code> is that I simply do not know how nested they will be. That is also why I don't simply use <code>Map&lt;String, String&gt;</code>.</p>
  
  <p>Is there a simple solution to receiving arbitrary, nested JSON data as part of the <code>RequestBody</code>, or am I forced to either write a large, strongly typed data model or accept it as an <code>Object</code>, convert to JSON, and do all my field validation somewhere else?</p>
  
  <p>Controller signature for reference:</p>
  
  <pre><code>@RequestMapping(value = "test/data", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
      @ResponseBody
      public ResponseEntity&lt;StorageData&gt; publishData(@RequestBody MyRequest requestBody) {
  </code></pre>
  
  <p>Here's some example JSON. Where things get dicey are in the <code>data</code> and <code>schema</code> section where they may contain multiple levels of nesting that I wouldn't be aware of:</p>
  
  <pre><code>{
      "data": {
          "display": "bilbo_baggins"
      },
      "resourceID": "0123456789",
      "schema": {
          "type": "record",
          "namespace": "com.org.test",
          "name": "mySchema",
          "fields": [{
              "name": "display",
              "type": "string"
          }]
      }
  }
  </code></pre>