0

I tried to connect to mongodb through Java using restful services. I got many exceptions. I'm using websphere Application server Liberty Profile.

This is my code in java class

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";
    }
}

This is Web.XML file

<?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>

These are the output on the console

[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
Community
  • 1
  • 1
ahmed
  • 1
  • 2

3 Answers3

0

this is an image of my working space

when i usied to connect to mongoDB from an simple java application it connected normally but these exception out when i tried to connect from Dynamic Web Project

ahmed
  • 1
  • 2
0

You have the mongo-java-driver-3.0.1.jar added to the Java Build settings of your Project. Adding the jar there only means that the class files will be found by the compiler. It does NOT mean that the class files from the JAR will be deployed into WebSphere.

To add a jar to a dynamic web project in order to have it deployed to WebSphere you have to add it to the dynamic web project using one of the following steps:

  • Copy the jar file into the WEB-INF/lib directory of your web project.
  • Select dynamic web project > right mouse button > Deployment Assembly > Add > Select JAR file

Hope that helps

UliL
  • 21
  • 2
0

See Creating Liberty applications that use MongoDB in the IBM knowledge center.

For starters it says to add the mongodb-2.0 feature to your liberty config.

"Liberty profile provides a mongodb-2.0 feature that you can use to configure MongoDB instances and associated database connections for your applications. Access to MongoDB connections is available either by Java Naming and Directory Interface (JNDI) lookup or resource injection, as with other product resources. All actual database manipulation is performed by the native com.mongodb API."