Reputation: 1045
I have the following code. I want to return all documents present in catalog
as a json response. I am able to print all documents using DBCursor
.
@Path("/allmusic")
public class GetAllMusic {
@Produces(MediaType.APPLICATION_JSON)
@GET
public void getAllSongs(@Context HttpHeaders httpHeaders) throws UnknownHostException {
DB db = (new MongoClient("localhost",27017)).getDB("sampledb");
DBCollection dbCollection = db.getCollection("catalog");
DBCursor cursor = dbCollection.find();
while(cursor.hasNext())
{
System.out.println(cursor.next());
}
}
}
How can I return all the documents as a json response? Pardon if my question is silly, I am still a beginner.
EDIT I have made following additions to the code:
GetAllMusic.java
@Path("/allmusic")
public class GetAllMusic {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/playlist")
public Response getAllSongs(@Context HttpHeaders httpHeaders)
throws UnknownHostException, JsonProcessingException {
DB db = (new MongoClient("localhost",27017)).getDB("xmusicdb");
DBCollection dbCollection = db.getCollection("catalog");
DBCursor cursor = dbCollection.find();
List<CatalogPojo> result = new ArrayList<>();
while(cursor.hasNext()) {
result.add(new CatalogPojo(cursor.next()));
}
String json = new ObjectMapper().writeValueAsString(result);
return Response.ok(json, MediaType.APPLICATION_JSON).build();
}
}
CatalogPojo.java
public class CatalogPojo {
private String title, artist, album, year;
/*CatalogPojo(String title, String artist, String album, String year){
}*/
public CatalogPojo(DBObject next) {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
}
http://localhost:xxxx/xmusic/allmusic/playlist On accessing this url I am getting a 404. I think there is something wrong with my pojo file or List<CatalogPojo>
Upvotes: 0
Views: 487
Reputation: 1045
@SOlsson solution is excellent but following code solves the problem in less number of lines. It responds with a valid json string.
@Path("/allmusic")
public class GetAllMusic {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/playlist")
public Response getAllSongs(@Context HttpHeaders httpHeaders) throws UnknownHostException {
DB db = (new MongoClient("localhost",27017)).getDB("musicdb");
DBCollection dbCollection = db.getCollection("catalog");
DBCursor cursor = dbCollection.find();
JSON json =new JSON();
@SuppressWarnings("static-access")
String serialize = json.serialize(cursor);
System.out.println(serialize);
return Response.ok(serialize, MediaType.APPLICATION_JSON).build();
}
}
Upvotes: 0
Reputation:
You're almost there. Try this:
@Path("v1")
public class GetAllMusic {
@GET
@Path("/allmusic")
@Produces(MediaType.APPLICATION_JSON)
public Response getAllSongs {
...
List<AppropriatePojo> result = new ArrayList<>();
while(cursor.hasNext()) {
result.add(new AppropriatePojo(cursor.next()));
}
String json = new ObjectMapper().writeValueAsString(result);
return Response.ok(json, MediaType.APPLICATION_JSON).build();
}
Then access localhost:xxxx/v1/allmusic either with your browser or with thje Chorme plugin PostMan.
Upvotes: 2