使用java访问mongodb时的异常

时间:2015-12-20 10:46:33

标签: mongodb java-ee websphere websphere-liberty

我尝试使用restful服务通过Java连接到mongodb。我有很多例外。我正在使用websphere应用程序服务器Liberty Profile。

这是我在java类中的代码

package com;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;


@Path("/helloworld")
public class HelloWorld {

    @GET
    public String sayHello(){   

        System.out.println("step 1 - try to create constructor of DB");

        System.out.println("1.1");

        MongoClient mongoClient = new MongoClient();
        System.out.println("1.2");

        MongoDatabase db = mongoClient.getDatabase("Sensors");
        System.out.println("1.3");

        return "Hello World";
    }
}

这是Web.XML文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

    <display-name>RESTWeb</display-name>

    <servlet>
        <servlet-name>REST</servlet-name>
        <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.HelloWorldConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>REST</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

这些是控制台上的输出

[ERROR   ] Error occurred during error handling, give up!
com/mongodb/MongoClient
[ERROR   ] SRVE0777E: Exception thrown by application class 'org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage:116'
java.lang.RuntimeException: org.apache.cxf.interceptor.Fault: com/mongodb/MongoClient
    at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:116)
    at [internal classes]
Caused by: org.apache.cxf.interceptor.Fault: com/mongodb/MongoClient
    at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:163)
    ... 1 more
Caused by: java.lang.NoClassDefFoundError: com/mongodb/MongoClient
    at com.HelloWorld.sayHello(HelloWorld.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.ibm.ws.jaxrs20.server.LibertyJaxRsServerFactoryBean.performInvocation(LibertyJaxRsServerFactoryBean.java:607)
    ... 1 more

[ERROR   ] SRVE0315E: An exception occurred: java.lang.Throwable: java.lang.RuntimeException: org.apache.cxf.interceptor.Fault: com/mongodb/MongoClient
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:4900)
    at [internal classes]
Caused by: java.lang.RuntimeException: org.apache.cxf.interceptor.Fault: com/mongodb/MongoClient
    at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:116)
    ... 1 more
Caused by: org.apache.cxf.interceptor.Fault: com/mongodb/MongoClient
    at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:163)
    ... 1 more
Caused by: java.lang.NoClassDefFoundError: com/mongodb/MongoClient
    at com.HelloWorld.sayHello(HelloWorld.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.ibm.ws.jaxrs20.server.LibertyJaxRsServerFactoryBean.performInvocation(LibertyJaxRsServerFactoryBean.java:607)
    ... 1 more

2 个答案:

答案 0 :(得分:0)

this is an image of my working space

当我从一个简单的java应用程序连接到mongoDB时,它正常连接但当我尝试从Dynamic Web Project连接时出现这些异常

答案 1 :(得分:0)

请参阅IBM知识中心的Creating Liberty applications that use MongoDB

对于初学者来说,它会将mongodb-2.0 feature添加到你的自由配置中。

&#34; Liberty概要文件提供了mongodb-2.0功能,您可以使用该功能为应用程序配置MongoDB实例和关联的数据库连接。与其他产品资源一样,可以通过Java命名和目录接口(JNDI)查找或资源注入来访问MongoDB连接。所有实际的数据库操作都由本机com.mongodb API执行。&#34;