title: How to properly receive urlencoded form body with Spring
body: <p>I've just started using Spring, and I'm trying to receive a form-urlencoded POST body in a rest controller, but I can't for the life of me get it to work. Here's my "Hello World"-esque controller:</p>
  
  <pre><code>@RestController
  public class MyController {
      @ResponseBody
      @RequestMapping(
          value = "/",
          method = RequestMethod.POST,
          consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
          produces = MediaType.TEXT_PLAIN_VALUE
      )
      public String index(@RequestBody String text) {
          return "Text: " + text;
      }
  }
  </code></pre>
  
  <p>I've tried many different variations, all with differing errors. The particular configuration above produces the following error when receiving a POST request with a "text" parameter from Postman.</p>
  
  <pre><code>Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.mywebsite.controllers.MyController.index(java.lang.String)
  </code></pre>
  
  <p>I've looked at many other stackoverflow posts about this topic and tried to implement their various solutions to no avail. Here's a list of the most promising ones:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/38960117/spring-json-request-body-not-mapped-to-java-pojo">Spring JSON request body not mapped to Java POJO</a>
  
  <ul>
  <li>This is the ideal outcome for me, a POJO with all the parameters. However, when I tried this, all POJO fields were null no matter what I passed in.</li>
  </ul></li>
  <li><a href="https://stackoverflow.com/questions/24551915/how-to-get-form-data-as-a-map-in-spring-mvc-controller">How to get Form data as a Map in Spring MVC controller?</a>
  
  <ul>
  <li>Following the first solution on that post, using a MultiValueMap produces the same error as above.</li>
  <li>In the second, the parameter map is empty every time.</li>
  </ul></li>
  <li><a href="https://stackoverflow.com/questions/31594608/how-to-retrieve-form-post-parameters-in-spring-controller">How to retrieve FORM/POST Parameters in Spring Controller?</a>
  
  <ul>
  <li>Produces an empty map, similar to the one above.</li>
  </ul></li>
  </ol>
  
  <p>There were a couple more that I can no longer find, and for most of these posts I've tried tweaking the annotations each time. I had great success when I tried GET and JSON POST requests, but for some reason these urlencoded requests refuse to work. </p>