4

I try to use Spring Restful webservice.

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.

But I got below exception.

org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type

I have tried in many ways but it was unsuccessful. Below is my code snippet.

Client Method :

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();
    }
}

Service Method :

@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;
    }

Track Class :

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 + "]";
    }
}

Also I tried with

restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

And

MultiValueMap<String, String> header = new LinkedMultiValueMap<String, String>(); 
header.add("Content-Type", "application/json");           
HttpEntity<Object> httpEntity = new HttpEntity<Object>(track, header); 
ResponseEntity<Track> response = restTemplate.exchange(uri, HttpMethod.POST, httpEntity, Track.class); 
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • Have a look at this answer. http://stackoverflow.com/questions/4075991/post-request-via-resttemplate-in-json Plus you can use "consumes" instead of specifying the headers in your rest api.Something like this: @RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType .APPLICATION_JSON_VALUE) – Sahil Nagpal Apr 03 '17 at 05:17
  • I tried answers from this URL, but it was unsuccessful. – Easy2DownVoteHard2Ans Apr 03 '17 at 05:19
  • Sorry, i used consumes = MediaType.APPLICATION_JSON_VALUE. But same issue. is there issue related to dependency jar files? – Easy2DownVoteHard2Ans Apr 03 '17 at 05:24
  • Turn up logging on `org.springframework.web` to DEBUG and see what media type is incoming. In particular, confirm it's not using form encoding. As a note, check into the newer Spring compound annotations; you could use `@RestController`, and you could simplify your mapping to `@PostMapping("/test")`. – chrylis -cautiouslyoptimistic- Apr 03 '17 at 05:33
  • Ok chrylis let me check with debugging. – Easy2DownVoteHard2Ans Apr 03 '17 at 05:46
  • I would like to know what version of Spring are you using? – SpinSage Apr 03 '17 at 07:24
  • Which web container are you using to deploy the service? Also could you provide the code for the Track class. – SpinSage Apr 03 '17 at 07:57
  • @ chrylis. i have printed httpheader in service, it is coming as application/json. and if i remove @RequestBody from postTrack method. no exception. but track object printed as Track [title=null, singer=null] – Easy2DownVoteHard2Ans Apr 03 '17 at 09:07
  • @NotionBit.. Please check updated Track class. I am using spring 4.1.6 & apache tomcat 7. – Easy2DownVoteHard2Ans Apr 03 '17 at 09:09

1 Answers1

2

Finally it is working by made changes as follows :

Added below lines in servlet xml.

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="messageConverters"> 
        <list> 
            <ref bean="jsonConverter" /> 
        </list> 
    </property> 
</bean> 

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
    <property name="supportedMediaTypes" value="application/json" /> 
</bean>

Add added jackson-core-2.5.0 & jackson-annotations-2.5.0 jars in classpath.