A sample application created using Jersey 1.0.1 and Mongodb 2.4.3.
The first step is to install Mongo DB using the instructions provided at MongoDB site. Mongo DB is one of the noSQL databases that boasts about improving performance when compared to the traditional databases.
To operate with the MongoDB, I used a MonjaDB plugin (http://www.jumperz.net/update/) that can be integrated with eclipse and you will be good to view/update the DB information.
The next step is to create a Jersery application which is essentially a RESTful service that would perform the normal CRUD operations on the Mongo DB. The main components that I would like to highlight are as follows
- Resource class (PersonResourceImpl.java) that contains the RESTful methods via find, create, update and delete respectively. This class is annotated as @Component
- Service class (PersonServiceImpl.java) that contains the business logic. This class is annotated as @Service
- Repository class (PersonRepositoryImpl.java) which has the main DB operations using MongoTemplate. This class is annotated as @Repository
- POJO class (Person.java) that is used as a data object and has two annotations @Document (to store as an object in MongoDB) and @XmlRootElement (for marshalling/unmarshalling JSON/XML response)
- web.xml: The servlet class used here is "com.sun.jersey.spi.spring.container.servlet.SpringServlet". Most of the examples had "com.sun.jersey.spi.container.servlet.ServletContainer" but this required explicit mentioning of the package in which Resource class resides in. But SpringServlet does not need it as it automatically scans and loads the Resource class.Also the explicit marshalling/unmarshalling was not required to convert the response to JSON/XML which most of the examples did by using "com.sun.jersey.api.json.POJOMappingFeature".
- applicationContext.xml: This contains the MongoTemplate definition which handles the connectivity with MongoDB. Here you can see that there is no specific db configuration required unlike other traditional databases which requires explicit mentioning of the driver, url etc. Only the database name "demo" is specified. Also there is no explicit definition of the Resource, Service and Repository classes as they are self annotated.
<servlet>
<servlet-name>JerseyWebApplication</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
</servlet>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongo" ref="mongo"/>
<constructor-arg name="databaseName" value="demo"/>
</bean>
Good Article...Clearly presented....
ReplyDeleteHi,
ReplyDeleteThank you for this tutorial, I just downloaded it imported it in my eclipse, run it, there is only an empty form and three buttons (Search, Add Person, Save), when I click nothing happened, I have MongoDB running in my machine, do we have to create the demo database ? do we have to insert some data before running it ? please your explanation is appreciated.
Thanks