How to return JSON from spring RESTful service and

2019-07-20 20:08发布

问题:

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"].

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.

Inside my @RestController class of my Spring RESTful web service

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

Inside my config-servlet.xml

<context:component-scan base-package="com.songs.service.controller" />
<mvc:annotation-driven />

Inside the controller of my spring MVC app calling this above RESTful web service

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

回答1:

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...

Then in your restfull ws

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

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";    

}



回答2:

you have to use model class like List

Please have a look this below source code in github. It has json response as object. Hope it will help. https://github.com/mohansaravanan/spring https://github.com/mohansaravanan/spring/tree/master/springmvc-3.2.2



标签: spring rest