MongoDB获取所有记录Java API

时间:2016-04-18 08:05:19

标签: java mongodb

我正在运行查询以从mongoDB获取特定集合的所有记录,代码为 -

@WebMethod(operationName = "getAllWorkItem", action = "urn:GetWorkItem")
@WebResult(name = "workItems")
public List<WorkItem> getAllWorkItem() {
    List<WorkItem> workItemList = new ArrayList<WorkItem>();

    WorkItem workItem;
    AttachmentTransactions atr = new AttachmentTransactions();
    AuditInfoTransactions autr = new AuditInfoTransactions();
    // documentWItem.put("id", workItemId);
    FindIterable<Document> item = transaction.getWorkItemCollection().find();

    for (Document document : item) {
        workItem = new WorkItem();
        @SuppressWarnings("unchecked")
        ArrayList<String> attchObjArr = (ArrayList<String>) document.get("attachmentId");
        workItem.setAttachment(atr.getAttachmentById("", attchObjArr.get(0)));

        @SuppressWarnings("unchecked")
        ArrayList<String> auditObjArr = (ArrayList<String>) document.get("auditInfoId");
        workItem.setAuditInfo(autr.getAuditInfoById("", auditObjArr.get(0)));

        workItem.setEtc(document.getDate("etc"));
        workItem.setExecutionUrl(document.getString("executionUrl"));
        workItem.setId(document.get("_id").toString());
        workItem.setName(document.getString("name"));
        workItem.setPriority(document.getString("priority"));
        workItem.setProcessName(document.getString("processName"));
        workItem.setStatus(document.getString("status"));
        workItem.setType(document.getString("type"));
        workItemList.add(workItem);
    }

    return workItemList;
}

但是我收到错误,我不明白这是一个问题,因为超时或我执行API的方式?任何帮助或指针将不胜感激。

Apr 18, 2016 1:31:56 PM org.apache.cxf.phase.PhaseInterceptorChain    doDefaultLogging
WARNING: Interceptor for   {http://ws.ss.inct.com/}WorkItemFacadeService#  {http://ws.ss.inct.com/}getAllWorkItem has thrown exception, unwinding   now
org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:81)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:240)
at org.apache.openejb.server.cxf.CxfWsContainer.onMessage(CxfWsContainer.java:89)
at org.apache.openejb.server.webservices.WsServlet.service(WsServlet.java:98)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:83)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.connector.ClientAbortException: java.net.SocketException: Broken pipe
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:407)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:480)
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:356)
at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:308)
at org.apache.catalina.connector.CoyoteOutputStream.close(CoyoteOutputStream.java:108)
at org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream.close(AbstractHTTPDestination.java:734)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
at org.apache.cxf.transport.http.AbstractHTTPDestination$BackChannelConduit.close(AbstractHTTPDestination.java:677)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
... 31 more
Caused by: java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:480)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366)
at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:240)
at org.apache.coyote.http11.filters.ChunkedOutputFilter.doWrite(ChunkedOutputFilter.java:119)
at org.apache.coyote.http11.AbstractOutputBuffer.doWrite(AbstractOutputBuffer.java:192)
at org.apache.coyote.Response.doWrite(Response.java:499)
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:402)
... 39 more

1 个答案:

答案 0 :(得分:1)

Another Way to to connect mongo db using java


    public static void main(String[] args) {

        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("mydb");// db name


         DBCollection collection = db.getCollection("test");
          BasicDBObject query = new BasicDBObject();
          BasicDBObject rmfld = new BasicDBObject();
         JSONArray jrr=new JSONArray();
         JSONObject jobj=new JSONObject();
         // query.put("name", "test");
          rmfld.put("_id", 0);

          try{
          DBCursor openCur = collection.find(query,rmfld);

            while (openCur.hasNext()) {
                jrr.put(openCur.next());
            }
            if(openCur!=null)
            {
                openCur.close();
            }
            System.out.println(jrr);
          }
            catch (Exception e) {
                // TODO: handle exception
            }

    }