Announcement Announcement Module
Collapse
No announcement yet.
Access a JSON from a spring RESTful Service, in a spring app by RestTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access a JSON from a spring RESTful Service, in a spring app by RestTemplate

    My Spring RESTful web service is returning a JSON form of-[{"key1":"value1","key2":"value2","key3":"value3"}, {"key4":"value4","key5":"value5","key6":"value6 "}]Now when my spring MVC app, try to access it, to show in a JSP then Exception occurs saying-no suitable HttpMessageConverter found Please help me where I going wrong.Here is my code-
    Inside @Controller class of my spring MVC app calling the RESTful service

    //**com.songs.controllers.FrontSongController.java** @RequestMapping(value="/alls",method=RequestMethod.POST) public String getAllSongs(ModelMap md) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); HttpEntity<String> entity = new HttpEntity<String>(headers); String url="http://localhost:7001/SongAppWS/songappWS/allsongsWS"; RestTemplate rt=new RestTemplate(); //SongResource.class is for representation on incoming JSON see below for its code //This is the line no 48 that you will see in below browser logs ResponseEntity<SongResource> listofallsongs=rt.exchange(url,HttpMethod.GET,entity, SongResource.class); md.addAttribute("listname", "Songs available in the repository:"); System.out.println("Response Entity object= "+listofallsongs); System.out.println("Response Entity body= "+listofallsongs.getBody().toString()); return "Sucess"; } Inside config-servlet.xml of my spring MVC app calling the RESTful service

    <context:component-scan base-package="com.songs.controllers" /> <mvc:annotation-driven /> <context:annotation-config/> <bean class="org.springframework.web.servlet.mvc.annotation.De faultAnnotationHandlerMapping"></bean> <bean class="org.springframework.web.servlet.mvc.annotation.An notationMethodHandlerAdapter"></bean> <bean class="org.springframework.web.client.RestTemplate"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJa cksonHttpMessageConverter"/> </list> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedView Resolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView " /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> Inside SongResource.java of my spring MVC app, which I am trying to use for converting the coming JSON to my SongResource.class object, that my spring MVC app can use in a jsp

    //**com.songs.service.resource.SongResource.java** public class SongResource { private String name; private String film; private String singer; public SongResource(String name,String film,String singer) { this.name=name; this.film=film; this.singer=singer; } //setter & getters of all above } On calling the spring REST service from my spring MVC app the browser is saying as below-

    Error 500--Internal Server Error org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.songs.service.resource.SongResource] and content type [application/json] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java :77) at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:619) at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:446) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:377) at com.songs.controllers.FrontSongController.getAllSongs(FrontSongController.java:48) //and so on
Working...
X