Archive
Mongo JEE [step6]
In [step5] we have modified our JAX-RS LogsService to returns List of Pojo by using :
- MongoJack to get List of Pojo from Mongo DB.
- Apache CXF JSONProvider based on Jettison to serialize the Pojo Log to JSON stream.
With Pojo Mapper MongoJack, the LogsService looks like this:
@GET @Path("/all") @Produces(MediaType.APPLICATION_JSON) public List<Log> findAll() { DB db = MongoHolder.connect().getDB("websites"); DBCollection dbColl = db.getCollection("logs"); JacksonDBCollection<Log, String> coll = JacksonDBCollection.wrap( dbColl, Log.class, String.class); return coll.find().toArray(); }
When I have studied how to use Mongo in JEE Application with JAX-RS, I have read the great article Modern Web Apps using JAX-RS, MongoDB, JSON, and jQuery. After reading this article, I told me « why we need to use a Pojo Mapper with JAX-RS although Mongo manages their data with BSON representation? », on other words why we could not develop our JAX-RS service like this :
@GET @Path("/all") @Produces(MediaType.APPLICATION_JSON) public DBCursor findAll() { DB db = MongoHolder.connect().getDB("websites"); DBCollection coll = db.getCollection("logs"); return coll.find(); }
Using DBCursor avoids to create a Pojo and avoids to serialize the Pojo to JSON. So I have decided to create Mongo JEE project. In this article we will modify our JAX-RS LogsService to use Mongo DBCursor in the service by using the JAX-RS provider of the Mongo JEE.
Mongo JEE [step5]
In [step4] we have tranformed our LogsServlet to a JAX-RS LogsService which uses JAX-RS StreamingOutput. At this step the service returns JAX-RS Response :
@Path("/logs") public class LogsService { @Path("/all") public Response findAll() { ... } }
It’s very difficult to use this service in other context than JAX-RS (like using the service in fat client, using the service in a JUnit etc). Imagine you wish to write some JUnits which tests that our LogsService service returns some logs. With our current logs service, it’s hard to write that :
@Test public void testNotEmptyLogs() { LogsService service = new LogsService(); Response jaxrsResponse = service.findAll(); // How to get the list of logs to test that logs are not empty? }
When you develop a service, the best mean is that you don’t link it to a framework. For instance :
- if your service returns list of Pojo :
@Path("/logs") public class LogsService { @Path("/all") public List<Log> findAll() { ... } }
you can write this test :
@Test public void testNotEmptyLogs() { LogsService service = new LogsService(); List<Pojo> pojoList = service.findAll(); Assert.assertFalse(pojoList.isEmpty()); }
- if your service returns a Mongo DBCursor :
@Path("/logs") public class LogsService { @Path("/all") public DBCursor findAll() { ... } }
you can write this test :
@Test public void testNotEmptyLogs() { LogsService service = new LogsService(); DBCursor cursor= service.findAll(); Assert.assertFalse(cursor.toArray().isEmpty()); }
With JAX-RS you can write services which returns anything (List of Pojo, Mongo DBCursor), but it’s not magic and you need JAX-RS provider to serialize (in our case) List of Pojo, DBCursor etc into JSON stream :
- in this article we will see how to manage JAX-RS service with Pojo. We will use MongoJack to get list of Log Pojo from Mongo DB and the Apache CXF JSONProvider, the JAX-RS provider to serialize Pojo to a JSON stream.
- in the next article [step6] we will see how to manage JAX-RS service with Mongo DBCursor by using the Mongo JEE JAX-RS providers.
Mongo JEE [step3]
In [step2] we have used mongo in a servlet to return the JSON array of the logs collection. In this article we will use Mongo JEE to improve it :
- use com.mongodb.jee.MongoHolder#connect() to avoid hard coding the mongo URI in the servlet.
- use com.mongodb.jee.util.JSON to manage JSON streaming when mongo cusor is serialized as JSON and is written in the HTTP response.
- use com.mongodb.jee.servlet.ServletHelper to simplify the code which set response as JSON and write the JSON stream in the HTTP response.
Mongo JEE [step2]
In [step1] we have seen how to execute mongo operations (find, insert, remove) with Java code by using Mongo Java Driver and the Pojo Mapper MongoJack. In this article we will create a WebApp application composed with a Servlet which connects to the Mongo DB and returns the JSON stream array of the logs collection in the HTTP response, on other words when the following URL will be called :
http://localhost:8081/mongo/servlet/logs/ the servlet will return the JSON stream array :
[ { "_id" : { "$oid" : "518d01813bfe01bef797d031"} , "url" : "http://www.mongodb.org/" , "created" : { "$date" : "2013-05-10T14:17:37.515Z"}} , { "_id" : { "$oid" : "518d01813bfe01bef797d032"} , "url" : "http://www.mongodb.org/" , "created" : { "$date" : "2013-05-10T14:17:37.515Z"}} , ...]
To do that, we will :
- use Embedded Jetty as HTTP server to start the WebApp with simple Java main.
- use Mongo Java Driver.
- create the LogsServlet, the servlet which returns the JSON stream array of the logs.
Mongo JEE [step1]
In [step0] we have introduced Mongo JEE articles, install and initialize Mongo DB websites database with Mongo Shell scripts. In this article we will do the same thing with Java code by using :
- the Mongo Java Driver.
- the Pojo Mapper MongoJack.
Mongo JEE [step0]
Several months ago, I have studied how to use Mongo DB in a JEE Application. I read the great article Modern Web Apps using JAX-RS, MongoDB, JSON, and jQuery which explains how to develop modern Web Apps :
- on client side, AJAX with jQuery is used to consume JSON by calling a REST service.
- on server side, REST service managed with JAX-RS produces JSON stream coming from MongoDB. The REST service calls MongoDB with mongo-jackson-mapper which gets JSON stream from MongoDB (which stores their data with BSON format which looks like JSON) and serializes the JSON to build a Pojo. The REST service returns the Pojo and JAX-RS implementation deserializes the Pojo to produce JSON for the jQuery client.
On other words, the server produces JSON and not the whole HTML page and the client consumes JSON to refresh the UI.
I like this architecture, but I tell me why we could to use directly the JSON stream from MongoDB instead of serialize/deserialize a Pojo. That’s why I have created Mongo JEE project which helps you to use Mongo in a JEE Application.
I have decided to write Mongo JEE articles which shows in action the feature of Mongo JEE. Those articles will explain step by step how to develop a Dojo Grid with pagination which consumes a JAX-RS REST service which produces JSON coming directly from the MongoDB with Java Driver and not with Pojo-Mapper. At the end of those articles we will have a paginated grid populated with logs data :
- step [1] : the user call the index.htm page.
- step [2] : the server returns the content of the index.htm page which contains the code of the Dojo grid (without the data).
- step [3] : once the dojo grid is built, it calls the REST service to populate the grid by sending on the HTTP Header the « Range » parameter (ex : items=0-9). This Range parameter tells to the server the from and to item index that grid needs to populate data.
- step [4] : the REST service request the MongoDB and returns the JSON coming from the MongoDB to the Web client. The dojo grid receives the JSON and populate the grid. When page link is clicked, the step [3] and [4] are re-played.
In this article, we will install and initialize MongoDB by creating a websites database which contains logs collection. A logs is just composed with date created and url.