Building Java Enteprise applications using MongoDB

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

This article is an excerpt from MongoDB for Java Developers book which I have recently published for Packt Publishing. You will find an example Java EE application designed to run on WildFly, using MongoDB as database.

Our application will be a simple bookstore which contains a list of book titles. A start-up class will be in charge to add books to the bookstore. Once executed, the user will be able to purchase books and to perform searches over the titles.

Every application starts from a proper data schema design so our first step will be defining the database structure and then we will create the Java EE interfaces, starting with those directly involved with data CRUD and then moving out to the customer view design.

Designing the schema

Our MongoDB documents will be created in the following storage:

  • Database: javaee7
  • Collection: bookstore

And here is the list of keys we will store in our MongoDB Document:

  • id (automatically generated)
  • title (String)
  • author (String)
  • type (String)
  • price (Integer)
  • copies (Integer)

And here is an example Document that we will use in our application:

{
        "_id" : ObjectId("5541ea47438724845af4cff7"),
        "title" : "The Hobbit",
        "author" : "J.R.R. Tolkien",
        "type" : "Fantasy",
        "price" : 8,
        "copies" : 10
}

All database objects will be created by our Java Enterprise application so right now your only concern will be starting up MongoDB.

Building up the Enterprise project with NetBeans

Our Java Enterprise application will now be created, step by step. 

We will use NetBeans for coding and building our project. Our helping hand will be Maven which is a de facto standard used by Java developers to arrange for a standard application structure, compile, deploy and test it. Being an extensible framework based on plugins, the capabilities of Maven can be even expanded far beyond the above points. First of all, however, we need to configure it so that it can be used in combination with WildFly application server. Next section details it:

Configuring WildFly on NetBeans

Since the release 8.2 of Netbeans you can direclty add the WildFly server to the list of available servers, without the need to download it as a separate plugin. Start NetBeans at first. From the left side of its GUI, select the Services Tab and right-click on Server, as shown by this picture: 

wildfly jboss mongodb java enterprise

Select Add Server. The server wizard will start: as first option choose “WildFly Application Server” from the list of available application server. Click on the Next button to move ahead to the next step:

wildfly jboss mongodb java enterprise

In the following window select the location where WildFly is installed and pick up the server configuration file to be used (standalone.xml is the default configuration file):

wildfly jboss mongodb java enterprise

Click on Finish. As you can see from the following picture, now the WildFly application server is enlisted among the available Services by expanding the Servers option:

wildfly jboss mongodb java enterprise

Creating our Java Enteprise Project

In order to create our project we will use a Maven project. Maven is a popular software and release management tool which buys you:

  • A standard structure for all your Java projects
  • A centralized and automatic management of dependencies

Most development environments are Maven friendly this means that you don’t need to download any additional plugin. Choose from the File menu select New Project. Select within the Maven categories Web Application as displayed by the following window:

wildfly jboss mongodb java enterprise

Choose Next from the navigation button. The following window will be displayed:

wildfly jboss mongodb java enterprise

Enter the Project Name, its location on your file system, the GroupId, Version and Package information. Then click on Next. The Group Id in Maven terms is a naming schema used by your Maven project. It generally matches with the root package of your application

In the following window, select as Server WildFly application server and Java EE 7 Web as Java version:

wildfly jboss mongodb java enterprise

Click on Finish to create the Maven project. The following basic structure should be available in your project view:

wildfly jboss mongodb java enterprise

Within the Project Files folder a file named pom.xml has been included. This file is the Maven project’s configuration file where you will be able to configure the dependencies on other libraries and also plugins.

Maven plugins are just Java libraries which can be used to empower your Maven with additional capabilities such as compiling, deploying and testing your code

In order to compile your project you will need to specify the dependencies, that is the library which needs to be used by our project. We will start with the following set of libraries in our project:

  • Enterprise Java Beans to code a Startup class in our project
  • Java Server Faces and Context Dependency Injection to code the Web application front-end and the Back end Beans
  • Mongo DB Java Driver for storing data
  • Google’s Gson for transforming JSON Strings into Java objects and viceversa

So here is the list of Dependencies which we need to include in the pom.xml file (you can just replace this with the default one that has been created by NetBeans):

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.wildfly.bom</groupId>
			<artifactId>jboss-javaee-7.0-with-all</artifactId>
			<version>${version.jboss.bom}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>
<dependencies>

	<dependency>
		<groupId>org.mongodb</groupId>
		<artifactId>mongo-java-driver</artifactId>
		<version>2.12.4</version>
	</dependency>

	<dependency>
		<groupId>javax.enterprise</groupId>
		<artifactId>cdi-api</artifactId>
		<scope>provided</scope>
	</dependency>

	<dependency>
		<groupId>org.jboss.spec.javax.faces</groupId>
		<artifactId>jboss-jsf-api_2.2_spec</artifactId>
		<scope>provided</scope>
	</dependency>

	<dependency>
		<groupId>org.jboss.spec.javax.ejb</groupId>
		<artifactId>jboss-ejb-api_3.2_spec</artifactId>
		<scope>provided</scope>
	</dependency>

	<dependency>
		<groupId>org.jboss.spec.javax.annotation</groupId>
		<artifactId>jboss-annotations-api_1.2_spec</artifactId>
		<scope>provided</scope>
	</dependency>

	<dependency>
		<groupId>com.google.code.gson</groupId>
		<artifactId>gson</artifactId>
		<version>2.3.1</version>
	</dependency>

</dependencies>

You can see we have also included a Bill of Materials (BOM) at the top of our dependencies. This is an handy option so that you don’t need to specify each of WildFly’s library version, as they are maintained in the external BOM file

We are done with the Project’s skeleton. Now for the real classes which will make up your application.

Follow us on Twitter