title: Spring RestTemplate 415 Unsupported Media Type
body: <p>I try to use Spring Restful webservice. </p>
  
  <p>I have created two projects in two eclipse. In one project I have written RestClient program and in another project I have written webservice and stared the webservice over tomcat. I am trying to pass java bean as json communication between client and server.</p>
  
  <p>But I got below exception.</p>
  
  <pre><code>org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type
  </code></pre>
  
  <p>I have tried in many ways but it was unsuccessful. Below is my code snippet.</p>
  
  <p>Client Method :</p>
  
  <pre><code>private static void postTrack() {
      try {
          final String uri = "http://localhost:8181/RestWS/test";
  
          Track track = new Track();
          track.setTitle("Singer");
          track.setSinger("Shas");
  
          RestTemplate restTemplate = new RestTemplate();
  
          Track responseTrack = restTemplate.postForObject(uri, track, Track.class);
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Service Method :</p>
  
  <pre><code>@RequestMapping(value = "/test", method = RequestMethod.POST, headers = "Accept=application/json")
      public @ResponseBody Track testMethod(@RequestBody Track track) {
          System.out.println(" Inside Test Method : ");
          System.out.println(" In GreetingController.greeting() "+track.getSinger());
          return track;
      }
  </code></pre>
  
  <p>Track Class :</p>
  
  <pre><code>public class Track {
  
      String title;
      String singer;
  
      public String getTitle() {
          return title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  
      public String getSinger() {
          return singer;
      }
  
      public void setSinger(String singer) {
          this.singer = singer;
      }
  
      @Override
      public String toString() {
          return "Track [title=" + title + ", singer=" + singer + "]";
      }
  }
  </code></pre>
  
  <p>Also I tried with</p>
  
  <pre><code>restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
  restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
  </code></pre>
  
  <p>And </p>
  
  <pre><code>MultiValueMap&lt;String, String&gt; header = new LinkedMultiValueMap&lt;String, String&gt;(); 
  header.add("Content-Type", "application/json");           
  HttpEntity&lt;Object&gt; httpEntity = new HttpEntity&lt;Object&gt;(track, header); 
  ResponseEntity&lt;Track&gt; response = restTemplate.exchange(uri, HttpMethod.POST, httpEntity, Track.class); 
  </code></pre>