如何从 Spring RESTful 服务返回 JSON 并使用 RestTemplate 类访问它 [英] How to return JSON from spring RESTful service and access it using RestTemplate class
问题描述
我制作了一个 spring RESTful web 服务,用于提供 JSON 格式的热门歌曲列表,为此我在列表中添加了歌曲名称,并从我的 Spring RESTful web 服务的 @Restcontroller 返回了它.所以 @RestController 将自动处理这个列表并转给我这个 JSON 形式 ["song1","song2","song3"].
I made a spring RESTful web service for giving list of top songs in JSON formate, for this I added song names in a List and I returned this from the @Restcontroller of my Spring RESTful web service.SO @RestController will automatically process this List and rturns me this JSON form ["song1","song2","song3"].
现在任何人都可以告诉我如何返回带有更多属性的歌曲名称,例如- (歌曲名称、电影、作词、歌手、总点击数)例如 -(Lungi Dance"、Chennai Express"、Honey Singh"、Honey Singh",5000).另外告诉我如何访问它,我的 spring MVC 应用程序使用 RestTemplate 调用此 Web 服务.强>请告诉我以下文件中的更改.
Now can any body tell me how I can return song names with some more attribute like - (Song Name , Film, Lyricist, Singer(s), Total hits) For Example - ("Lungi Dance", "Chennai Express", "Honey Singh", "Honey Singh", 5000).Also tell me how can I access it my spring MVC application calling this web service using RestTemplate. Please tell me the changes in below files.
在我的 Spring RESTful Web 服务的 @RestController 类中
@RequestMapping(value = "/topsongsWS", headers="Accept=application/json")
Public List<?> getTopSongsWS()
{
List<String> l1 = new ArrayList<String>();
l1.add("mann mera (Table No 21)");
l1.add("Lazy lad (Ghanchakkar)");
l1.add("Oye boy Charlie (Matru Ki Bijli Ka Mandola)");
l1.add("Blue Hai Pani Pani");
l1.add("Latt lag gayi (Race 2)");
return l1;
}
在我的 config-servlet.xml 中
<context:component-scan base-package="com.songs.service.controller" />
<mvc:annotation-driven />
在我的 spring MVC 应用程序的控制器中调用上面的 RESTful Web 服务
@RequestMapping(value="/topsongs",method=RequestMethod.POST)
public String getTopSongs(ModelMap md)
{
//did stuff to configure headers & entity
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
//this is the URL of my RESTfulservice returning JSON format ["song1","song2","song3"]
String url="http://localhost:7001/SongAppWS/songappWS/topsongsWS";
RestTemplate rt=new RestTemplate();
ResponseEntity<?> listoftopmovies=rt.exchange(url,HttpMethod.GET,entity, List.class);
md.addAttribute("listname", "Top 5 Songs of The Week:");
String response=listoftopmovies.getBody().toString();
List<String> listed = new ArrayList<String>(Arrays.asList(response.split(", ")));
md.addAttribute("res",listed);
return "Sucess";
}
推荐答案
要返回具有更多属性的歌曲,您应该创建一个资源表示类.在您的情况下,它可能看起来像
To return your songs with more attribute, you should create a resource representation class.In your case,it could look like
class Song {
private String name
private String film
//other fields and methods...
然后在你的休息区
@RequestMapping(value = "/topsongsWS", headers="Accept=application/json")
public List<?> getTopSongsWS()
{
List<Song> l1 = new ArrayList<Song>();
l1.add(new Song(atr1,atr2....));
l1.add(new Song(atr1,atr2....));
l1.add(new Song(atr1,atr2....));
return l1;
}
在你的 spring mvc 应用程序中,你也应该有资源表示类,响应类型现在是 Song
而不是 String
要消耗你的 ws,这应该可以工作
In your spring mvc app, you should have too the resource representation class, and the response type will now be Song
instead of String
To consume your ws, this should work
@RequestMapping(value="/topsongs",method=RequestMethod.POST)
public String getTopSongs(ModelMap md)
{
String url="http://localhost:7001/SongAppWS/songappWS/topsongsWS";
RestTemplate rt=new RestTemplate();
Song[] songs = template.getForObject(url, Song[].class);
md.addAttribute("listname", "Top 5 Songs of The Week:");
md.addAttribute("res", Arrays.asList(songs));
return "Sucess";
}
这篇关于如何从 Spring RESTful 服务返回 JSON 并使用 RestTemplate 类访问它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!