询问如何在java中的RESTful Web服务中连接数据库

时间:2023-01-27 12:13:02

I am doing a RESTful web service in java. But I can't run it successfully. exception occurs. Could anyone give my suggestion? I use mongo db as a database

我在java中做一个RESTful Web服务。但我无法成功运行它。异常发生。谁能提出我的建议?我使用mongo db作为数据库

@Path("/test")
public class test {

    mongoDAO mongodao;

    @GET
    @Produces("text/html,text/plain")
    public String getContainer(){

        mongoDAOImpl impl=new mongoDAOImpl();
        Mongo mongo=impl.getConnection("127.0.0.1","27017");
        DB db=impl.getDataBase(mongo,"public");
        DBCollection coll=impl.getColl(db,"public");
        mongoDTO dto=new mongoDTO();
        dto.setParentpath("/home/public/liren");
        dto.setUserName("liren");
        dto.setPassWord("liren");

   // System.out.println("In PathResource.getContainer, path is: " + path);
    //print headers for debug
  //  for (String hdr : headers.getRequestHeaders().keySet()) {
  //    System.out.println("Hdr: "+ hdr + " - " + headers.getRequestHeader(hdr));
  //  }

    try{
        //DBObject dbobject=mongodao.findOneRecord(coll, dto);
        DBCursor dbcur=mongodao.findAllRecords(coll);
        while(dbcur.hasNext()){
            System.out.println(dbcur.next());
        }
//        if(dbobject==null){
//            return Response.status(Response.Status.NOT_FOUND).build();
//        }else{
            //return Response.ok().header("Specification-version", "1.0").build();
        return "Ok";
//        }
    }
        catch(Exception ex){
                ex.printStackTrace();
        //return Response.status(Response.Status.NOT_FOUND).tag("Container Read Error : " + ex.toString()).build();
                return "Sorry Exception, try again!";
    }
    }
}

1 个解决方案

#1


0  

My guess is that mongodao is null, thus DBCursor dbcur=mongodao.findAllRecords(coll); will throw a NullPointerException.

我的猜测是mongodao为null,因此DBCursor dbcur = mongodao.findAllRecords(coll);将抛出NullPointerException。

#1


0  

My guess is that mongodao is null, thus DBCursor dbcur=mongodao.findAllRecords(coll); will throw a NullPointerException.

我的猜测是mongodao为null,因此DBCursor dbcur = mongodao.findAllRecords(coll);将抛出NullPointerException。