使用MongoDb的Jersey发送Json响应

时间:2017-09-27 15:40:49

标签: java json mongodb rest jersey

我有以下代码。我想将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访问此网址时,我收到了404.我认为我的pojo文件或List<CatalogPojo>

有问题

2 个答案:

答案 0 :(得分:2)

你快到了。试试这个:

@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。

答案 1 :(得分:0)

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