fr.opensagres.mongodb:mongo-jee

Web support + JAX-RS support for MongoDB.

License

License

Categories

Categories

MongoDB Data Databases
GroupId

GroupId

fr.opensagres.mongodb
ArtifactId

ArtifactId

mongo-jee
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Web support + JAX-RS support for MongoDB.

Download mongo-jee

How to add to project

<!-- https://jarcasting.com/artifacts/fr.opensagres.mongodb/mongo-jee/ -->
<dependency>
    <groupId>fr.opensagres.mongodb</groupId>
    <artifactId>mongo-jee</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/fr.opensagres.mongodb/mongo-jee/
implementation 'fr.opensagres.mongodb:mongo-jee:1.0.0'
// https://jarcasting.com/artifacts/fr.opensagres.mongodb/mongo-jee/
implementation ("fr.opensagres.mongodb:mongo-jee:1.0.0")
'fr.opensagres.mongodb:mongo-jee:jar:1.0.0'
<dependency org="fr.opensagres.mongodb" name="mongo-jee" rev="1.0.0">
  <artifact name="mongo-jee" type="jar" />
</dependency>
@Grapes(
@Grab(group='fr.opensagres.mongodb', module='mongo-jee', version='1.0.0')
)
libraryDependencies += "fr.opensagres.mongodb" % "mongo-jee" % "1.0.0"
[fr.opensagres.mongodb/mongo-jee "1.0.0"]

Dependencies

provided (4)

Group / Artifact Type Version
org.mongodb : mongo-java-driver jar 2.10.1
javax.servlet : servlet-api jar 2.5
javax.ws.rs : jsr311-api jar 1.1.1
javax.xml : jaxb-api jar 2.1

test (4)

Group / Artifact Type Version
junit : junit jar 4.8.1
de.flapdoodle.embed : de.flapdoodle.embed.mongo jar 1.27
org.apache.cxf : cxf-rt-frontend-jaxrs jar 2.7.0
org.eclipse.jetty : jetty-webapp jar 7.4.2.v20110526

Project Modules

There are no modules declared in this project.

What is Mongo JEE?

[Mongo Java Driver] (https://github.com/mongodb/mongo-java-driver) gives you the capability to connect and use NoSQL MongoDB with Java code.

Mongo JEE is a JAR which provides simply but usefull Java classes to use this driver in JEE context like using JAX-RS with com.mongodb.DBObject, connect/disconnect to Mongo with ServletContextListener, etc

Features

Here the list of the features provided by Mongo JEE:

JAX-RS support for MongoDB

[JAX-RS support for MongoDB] (https://github.com/angelozerr/mongo-jee/wiki/JAX-RS-support-for-MongoDB) to use Mongo Java structures (DBObject, DBCursor, etc) in the methods of your REST service. Here a sample service which returns the com.mongodb.DBObject :

  	@Path("/")
  	public class ProductService {

  	  	@GET
  	  	@Path("/findOne")
  	  	@Produces(MediaType.APPLICATION_JSON)
  	  	public DBObject findOne(){
  	  	  DB db = mongo.getDB("ecommerce");
  	  	  DBCollection col = db.getCollection("products");
      	  return col.findOne();            
        }
  	}

The JAX-RS support provides [several JAX-RS Provider] (https://github.com/angelozerr/mongo-jee/tree/master/mongo-jee/src/main/java/com/mongodb/jee/jaxrs/providers) which serialize/deserialize DBObject, DBCursor, etc to JSON stream.

##JSON streaming

JSON streaming : Mongo Java Driver provides com.mongodb.util.JSON helper to serialize DBObject, DBCursor etc to JSON stream but it works only with StringBuilder. Here a sample to write DBObject in HTTP response Write

    HttpServletResponse response = ...
    DBObject o = ...
    // Serialize the JSON DBOBject in a String
    StringBuilder json = new StringBuilder()
    com.mongodb.util.JSON.serialize(o, json);
    // Write the JSON string
    response.getWriter().write(json.toString());

Mongo JEE provides com.mongodb.jee.util.JSON which works with Writer/OutputStream:

    HttpServletResponse response = ...
    DBObject o = ...
    // Serialize the JSON DBOBject in the HTTP response Writer
    com.mongodb.jee.util.JSON.serialize(o, response.getWriter());

This idea was suggested to Mongo Java Driver in the JAVA-709 issue

MongoHolder

Mongo Java Driver provides com.mongodb.Mongo.Holder helper to hold several static Mongo instances for several Mongo uri. However, this holder doesn't manage deconnection, default Mongo instance.

Mongo JEE provides the com.mongodb.jee.MongoHolder which provides several features like deconnection, default Mongo instance.

Please read Using MogoHolder section for more information.

Initialize Mongo with ServletContextListener

[Initialize Mongo with ServletContextListener] (https://github.com/angelozerr/mongo-jee/wiki/Initialize-Mongo-with-ServletContextListener):

  	<listener>		
  	  <listener-class>com.mongodb.jee.servlet.MongoServletContextListener
   	  </listener-class>
  	</listener>

  	<context-param>
  	  <param-name>mongoURI</param-name>
  	  <param-value>mongodb://localhost:12345</param-value>
  	</context-param>

When MongoServletContextListener is started it initializes default Mongo connection with com.mongodb.jee.MongoHolder. After that, you can retrieve this Mongo .connection anywhere in your code like this:

  	Mongo mongo = MongoHolder.connect();

Pagination

Pagination to help pagination with MongoDB and client (like Dojo) to consumes the paginated list.

Download

Only source are available for this moment.

License

Apache License, Version 2.0

Article

You can read following articles about Mongo JEE.

Demo

Mongo JEE provides the [Mongo JEE Demo with Apache CXF and Dojo] (https://github.com/angelozerr/mongo-jee/wiki/Mongo-JEE-Demo-with-Apache-CXF-and-Dojo) which uses the features of Mongo JEE. This demo displays a paginated list of products. This WebApps deploys a JAX-RS REST service ProductsService which returns the paginated JSON products:

WebApps Overview

Versions

Version
1.0.0