title: How can I retrieve the raw JSON payload passed to a Spring Boot POST API?
body: <p>I am working on a <strong>Spring Boot</strong> project in which I have to develop an API handling <strong>POST</strong> request that contains a JSON payload.</p>
  
  <p>I was looking to this example documentation: 
  <a href="https://www.baeldung.com/spring-resttemplate-post-json" rel="nofollow noreferrer">https://www.baeldung.com/spring-resttemplate-post-json</a></p>
  
  <p>in which it is done:</p>
  
  <pre><code>@PostMapping(
    value = "/createPerson", consumes = "application/json", produces = "application/json")
  public Person createPerson(@RequestBody Person person) {
      return personService.saveUpdatePerson(person);
  }
  </code></pre>
  
  <p>Basing on my understanding this API is expecting a JSON payload into the body of the request (because I have the <strong>consumes = "application/json"</strong> into the <strong>@PostMapping</strong> annotation) that will be automatically converted into an instance of <strong>Person</strong> class.</p>
  
  <p>Is it this reasoning correct?</p>
  
  <p>If my reasoning is correct I have a doubt: my specific use case is pretty different: my API doesn't know what kind of object represents the JSON payload (for example it could be a <strong>Person</strong>, an <strong>Animal</strong> or a <strong>Thing</strong>). So the API can't figured out in what kind of object serialize the received JSON.</p>
  
  <p>So what I was thinking to do is:</p>
  
  <ol>
  <li>The API have to receive the raw JSON payload in some way.</li>
  <li>Basing on the JSON content it creates the appropriate object (for example an instance of <strong>Person</strong> or an instance of <strong>Animal</strong>).</li>
  </ol>
  
  <p>It could be a smart solution? 
  What have I to do to retrieve the raw JSON payload?</p>