title: Spring rest json post null values
body: <p>I have a Spring rest endpoint doing a simple hello app. It should accept a {"name":"something"} and return "Hello, something".</p>
  
  <p>My controller is:</p>
  
  <pre><code>@RestController
  public class GreetingController { 
  
      private static final String template = "Hello, %s!";
  
      @RequestMapping(value="/greeting", method=RequestMethod.POST)
      public String greeting(Person person) {
          return String.format(template, person.getName());
      }
  
  }
  </code></pre>
  
  <p>Person:</p>
  
  <pre><code>public class Person {
  
      private String name;
  
      public Person() {
          this.name = "World";
      }
  
      public Person(String name) {
          this.name = name;
      }
  
      public String getName() {
          return this.name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <p>When I make a request to the service like </p>
  
  <pre><code>curl -X POST -d '{"name": "something"}' http://localhost:8081/testapp/greeting
  </code></pre>
  
  <p>I get</p>
  
  <pre><code>Hello, World!
  </code></pre>
  
  <p>Looks like it isn't deserializing the json into the Person object properly. It's using the default constructor and then not setting the name. I found this: <a href="https://stackoverflow.com/questions/21417115/how-to-create-a-post-request-in-rest-to-accept-a-json-input">How to create a POST request in REST to accept a JSON input?</a> so I tried adding an @RequestBody on the controller but that causes some error about "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported". I see that is covered here: <a href="https://stackoverflow.com/questions/33796218/content-type-application-x-www-form-urlencodedcharset-utf-8-not-supported-for">Content type &#39;application/x-www-form-urlencoded;charset=UTF-8&#39; not supported for @RequestBody MultiValueMap</a> which suggests removing the @RequestBody</p>
  
  <p>I have tried removing the default constructor which it doesn't like either.</p>
  
  <p>This question covers null values <a href="https://stackoverflow.com/questions/19315662/rest-webservice-using-spring-mvc-returning-null-while-posting-json">REST webservice using Spring MVC returning null while posting JSON</a> but it suggests adding @RequestBody but that conflicts with above...</p>