title: How to get Spring controller to accept multiple types of JSON
body: <p>I have a Spring REST service (written in Groovy; although that shouldn't matter) that exposes a secured endpoint like so:</p>
  
  <pre><code>class AppData {
      String id
      String payload
      String fizzbuzz
  }
  
  @RequestMapping(method=RequestMethod.POST)
  @ResponseStatus(value = HttpStatus.OK)
  @ResponseBody AppResponse onAppData(@RequestBody AppData appData) {
      // Does some processing on appData and then saves
      // its 'payload' field to a MongoDB
  }
  </code></pre>
  
  <p>And this works perfectly fine when it receives JSON of the following form:</p>
  
  <pre><code>{
      "id" : "12345",
      "payload" : "Please save me to a MongoDB",
      "fizzbuzz" : "wakka wakka"
  }
  </code></pre>
  
  <p>I'm using Jackson to handle JSON serialization here.</p>
  
  <p>The problem is that I now want the <code>payload</code> property to be true JSON, not a String. So I want to be able to send the endpoint something like this:</p>
  
  <pre><code>{
      "id" : "12345",
      "payload" : {
          "foo" : 24,
          "bar" : false,
          "whistlefeather" : "Yes of course"
      },
      "fizzbuzz" : "wakka wakka"
  }
  </code></pre>
  
  <p>And have the payload properly saved off to MongoDB. But here's the catch:</p>
  
  <p>Lots of different teams (potentially hundreds) are going to be sending this endpoint <code>AppData</code>, and all of their payloads are going to look completely different (each team has different payloads they want to send me and have saved in MongoDB). And I don't want to have to write a new endpoint and <code>AppData</code> subclass for each new team I onboard (and thus expose this endpoint to).</p>
  
  <p>Is there any way I can:</p>
  
  <ul>
  <li>Keep my endpoint exactly the way it is; but</li>
  <li>Allow the <code>AppData#payload</code> property to accept JSON in any form?</li>
  </ul>
  
  <p>Keeping my code exactly the way it is, if I send a JSON message where the <code>payload</code> is also JSON (and isn't a string), Spring just returns a 400 with an empty response body. This is happening because Spring expects <code>payload</code> to be a String, but sees that its a JSON object.</p>
  
  <p>Any ideas on what I can do inside the <code>AppData</code> class to handle this, or any notions of what the best practice is here?</p>