Mongo JEE [step6]

mai 15, 2013 1 commentaire

In [step5] we have modified our JAX-RS LogsService to returns List of Pojo by using :

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.

Lire la suite…

Catégories:Apache CXF, Mongo JEE, MongoDB

Mongo JEE [step5]

mai 14, 2013 2 commentaires

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.

Lire la suite…

Catégories:Apache CXF, Mongo JEE, MongoDB

Mongo JEE [step4]

mai 14, 2013 6 commentaires

In [step3] we have improved our LogsServlet by using Mongo JEE. Servlet works well but it’s an old school mean.

Using servlet in this case will causes problem when you will wish to manage several operations (find all logs, find paginated logs, insert logs, etc). To manage that, you will have to create a servlet per operations or manage this dispatch at hand (for instance with a HTTP request dispatch parameter) like this :

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  DBCursor cursor = null;
  String dispatch = request.getParameter("dispatch");
  if ("all".equals(dispatch) {
	DB db = mongo.getDB("websites");
	DBCollection coll = db.getCollection("logs");

	DBCursor cursor = coll.find();
	ServletHelper.writeJson(cursor, response);
  } else if ("page".equals(dispatch) {
     ...
  }
}

It exists a lot of Java Web Framwework (Spring MVC, GWT, Wicket, Play!, JSF, Struts2, etc) which provides a controller framework which fix this problem. JAX-RS provides too an elegant mean to fix that : you develop a service class and you create a method for each operations. Here a JAX-RS sample :

@Path("/logs")
public class LogsService {

	@Path("/all")
	public Response findAll() {
	...
	}
	
	@Path("/page")
	public Response findPage() {
	...
	}
	
}

Once this JAX-RS service will be deployed, you will able to call :

In this article we will transform our LogsServlet with JAX-RS LogsService. We will use Apache CXF as JAX-RS implementation.

Lire la suite…

Catégories:Apache CXF, Mongo JEE

Mongo JEE [step3]

mai 12, 2013 2 commentaires

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.

Lire la suite…

Catégories:Mongo JEE, MongoDB

Mongo JEE [step2]

mai 11, 2013 2 commentaires

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 :

Lire la suite…

Catégories:MongoDB

Mongo JEE [step1]

mai 10, 2013 5 commentaires

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 :

Lire la suite…

Catégories:Dojo, Mongo JEE, MongoDB

Mongo JEE [step0]

mai 10, 2013 2 commentaires

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.

Lire la suite…

Catégories:Apache CXF, Dojo, Mongo JEE, MongoDB
Suivre

Recevez les nouvelles publications par mail.

Rejoignez 170 autres abonnés