title: Send POST and PUT through RestTemplate to a Spring Data Rest Api
body: <p>I've been developing a cloud app to mess a little with Spring Cloud and such. Now I'm stuck trying to send a POST or a PUT request to a Spring Data Rest backend using the RestTemplate API but everything I tried ends with an error: HttpMessageNotReadableException: Cannot deserialize instance of java.lang.String out of START_OBJECT token, HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_ARRAY token, ...from request with content type of application/xml;charset=UTF-8!, Error 400 null... you name it. After researching I discovered that it actually is quite hard to consume HAL JSON with RestTemplate (level 3 JSON hypermedia if I recall correctly) but I want to know if it is possible.</p>
  
  <p>I'd like to see some working (detailed if possible) examples of a RestTemplate sending POST and PUT to a Spring Data Rest backend. </p>
  
  <p>Edit: I tried postForEntity, postForLocation, exchange and it just ended in different kinds of errors. Those are some snippets I tried (there're more, it's just that I dispose them).</p>
  
  <p>My entity:</p>
  
  <pre><code>@Entity
  public class Account implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  
  private String name;
  
  @NotNull
  @NotEmpty
  private String username;
  
  @NotNull
  @NotEmpty
  private String authorities;
  
  @NotNull
  @NotEmpty
  private String password;
  
  //Constructor, getter and setter
  </code></pre>
  
  <p>Some restTemplate attemps:</p>
  
  <pre><code>    public Account create(Account account) {
      //Doesnt work :S
      MultiValueMap&lt;String, String&gt; map = new LinkedMultiValueMap&lt;String, String&gt;();
      map.add("name", account.getName());
      map.add("username", account.getUsername());
      map.add("password", account.getPassword());
      map.add("authorities", account.getAuthorities());
  
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_JSON);
      final HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt; entity = new HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt;(map,
              headers);
  
      return restTemplate.exchange(serviceUrl + "/accounts", HttpMethod.POST, entity, Account.class).getBody();
  }
  
  //Also tried with a AccountResource which extends from ResourceSupport and doesn't work either. This one gives me a error saying it cannot deserialize Account["name"].
  </code></pre>
  
  <p>Also tried like this and got an error about header being application/xml: <a href="https://stackoverflow.com/questions/32589980/resttemplate-posting-entity-with-associations-to-spring-data-rest-server">RestTemplate POSTing entity with associations to Spring Data REST server</a></p>
  
  <p>The other ones just repeat one of those errors.</p>