Web services 如何使用RestTemplate访问SpringMVC应用程序中的巨大JSON(来自SpringRESTful服务)

Web services 如何使用RestTemplate访问SpringMVC应用程序中的巨大JSON(来自SpringRESTful服务),web-services,spring-mvc,restful-url,resttemplate,Web Services,Spring Mvc,Restful Url,Resttemplate,我的Spring RESTful web服务返回一个JSON格式的- [{“键1”:“值1”,“键2”:“值2”,“键3”:“值3”},{“键4”:“值4”,“键5”:“值5”,“键6”:“值6”}] 现在,当我的spring MVC应用程序尝试访问它时,在JSP中显示,然后出现异常,说-找不到合适的HttpMessageConverter请帮助我哪里出错了。这是我的代码- 调用RESTful服务的我的spring MVC应用程序的@Controller类内部 //**com.songs.con

我的Spring RESTful web服务返回一个JSON格式的- [{“键1”:“值1”,“键2”:“值2”,“键3”:“值3”},{“键4”:“值4”,“键5”:“值5”,“键6”:“值6”}] 现在,当我的spring MVC应用程序尝试访问它时,在JSP中显示,然后出现异常,说-找不到合适的HttpMessageConverter请帮助我哪里出错了。这是我的代码-

调用RESTful服务的我的spring MVC应用程序的@Controller类内部

//**com.songs.controllers.FrontSongController.java**
</*
author Rohit Tiwari
*/>
@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";    
}
<context:component-scan base-package="com.songs.controllers" />
<mvc:annotation-driven />
<context:annotation-config/> 
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean  class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
        </list>
    </property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
在从我的spring MVC应用程序调用spring REST服务时,浏览器的说明如下-

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)

//等等

试试这个,希望它能帮助你

@RequestMapping(value="/alls",method=RequestMethod.POST)
public String getAllSongs(ModelMap md)
{  
String url="http://localhost:7001/SongAppWS/songappWS/allsongsWS";
RestTemplate rt=new RestTemplate();
SongResource[] songRs = template.getForObject(url, SongResource[].class);

List<SongResource> songs = Arrays.asList(songRs);

md.addAttribute("listname", "Songs available in the repository:");
md.addAttribute("listValues", songs);  
return "Sucess";    
}
@RequestMapping(value=“/alls”,method=RequestMethod.POST)
公共字符串getAllSongs(ModelMap md)
{  
字符串url=”http://localhost:7001/SongAppWS/songappWS/allsongsWS";
RestTemplate rt=新的RestTemplate();
SongResource[]songRs=template.getForObject(url,SongResource[].class);
List songs=Arrays.asList(songRs);
md.addAttribute(“列表名”,“存储库中提供的歌曲:”);
md.addAttribute(“listValues”,歌曲);
返回“成功”;
}
@RequestMapping(value="/alls",method=RequestMethod.POST)
public String getAllSongs(ModelMap md)
{  
String url="http://localhost:7001/SongAppWS/songappWS/allsongsWS";
RestTemplate rt=new RestTemplate();
SongResource[] songRs = template.getForObject(url, SongResource[].class);

List<SongResource> songs = Arrays.asList(songRs);

md.addAttribute("listname", "Songs available in the repository:");
md.addAttribute("listValues", songs);  
return "Sucess";    
}