[英]Send Json response using Jersey from MongoDb
我有以下代碼。 我想返回catalog
存在的所有文檔作為json響應。 我可以使用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());
}
}
}
如何返回所有文檔作為json響應? 如果我的問題很傻,請原諒。
編輯我對代碼做了以下補充:
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訪問此URL時,我得到的是404。我認為我的pojo文件或List<CatalogPojo>
你快到了。 嘗試這個:
@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();
}
然后使用瀏覽器或Chorme插件PostMan訪問localhost:xxxx / v1 / allmusic。
@SOlsson解決方案非常好,但是下面的代碼以較少的行數解決了該問題。 它以有效的json字符串響應。
@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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.