Saturday, November 3, 2012

Java Restful Example

REST, for "Representational State Transfer," is a lightweight architecture for network applications that use HTTP requests to read and write data

Java API for RESTful Web Services (JAX-RS), is a set if APIs to developer REST service. JAX-RS is part of the Java EE6, and make developers to develop REST web application easily.

In this examples we use Jersey popular JAX-RS implementation. Example shows how to develop a simple REST webservice using Jersey Client APIs.  We will use Jersey Client APIs to create a RESTful Java client to perform "GET" and "POST" to the REST service.

Jersey uses Jackson to convert object  to/from JSON. In this example we will convert object to JSON.

1. Jersey jars
pom.xml

2. GET Request


In JAX-RS, you can use @Path to bind URI pattern to a Java method.It  support complex URI matching with regular expression.

Annotate the method with @Produces(MediaType.APPLICATION_JSON). Jersey will use Jackson to handle the JSON conversion automatically.

Here we are converting Track object to JSON format and return back to user. To make Jersey support this JSON conversion we have to use jersey-json.jar  in Maven pom.xml as shown above.

To integrate JSON with Jersey we have to add few parameters to web.xml.

web.xml

In web.xml, declares "com.sun.jersey.api.json.POJOMappingFeature" as "init-param" in Jersey mapped servlet. It will make Jersey support JSON/object mapping.

Access Above REST service in two ways as shown below:

1) We can send "GET" request by hitting the url on the browser    http://localhost:8080/JavaRest2/json/song/get

2) Jersey client to send "GET" request and print out the returned JSON data.


Output from Server:

{"title":"Satyam Sivam Sundaram","singer":"Latha Mangeshkar"}

3. POST Request

@Consumes(MediaType.APPLICATION_JSON) will convert JSON data to object.


Jersey client to send a “POST” request, with json data and print out the returned output.


Output from Server ....

Track saved Track [title=Friday, singer=Rebacca]

Access Source Code:

Friday, October 26, 2012

Spring MVC using MongoDB as Database

In this tutorial we will create a simple Spring MVC 3 application that uses a document-oriented database for its persistence layer. We will be using MongoDB as our database. We will explore and discover how easy it is to integrate MongoDB with Spring MVC 3. Our application is a simple CRUD service for managing a list of Persons. We will provide functionalities for adding, deleting, editing, and viewing of all registered persons.

MongoDB
These are few benefits with MongoDB.
  • Document-oriented storage
  • Full Index Support
  • Replication & High Availability
  • Scale horizontally without compromising functionality.
  • Rich, document-based queries.
  • Atomic modifiers for contention-free performance.
  • Flexible aggregation and data processing.
  • Store files of any size without complicating your stack.
  • Enterprise class support, training, and consulting available. 
In a nutshell MongoDB uses JSON instead of SQL There's no static schema to create. All schemas are dynamic, meaning you create them on-the-fly. You can try a real-time online shell for MongoDB at http://try.mongodb.org/.

In order to complete this tutorial, you will be have to install a copy of MongoDB. You can download from http://www.mongodb.org/display/DOCS/Quickstart. The installation is really easy.

MongoDB is used if there are more number of write than reads. Some thing like Audit logs which will have frequent writes than reads and also huge data will be stored.
pom.xml 
(download source code link is provided for complete project access)


Lets start with MongoDBFactory.

MongoDBFactory.java


method "getMongo()" will be used to  get Mongo DB reference using host and port.
method "getDB()" Will be used to get reference to your DB created. This is some thing like MySql
"create database myDatabase".  This method is trying to get "myDatabase" from MongoDB.
method "getCollection()"  collections are like tables in mongodb stored as key value pairs.

Instead of factory if you want to write the same code it will look like below.
// Retrieve a db
Mongo m = new Mongo( "localhost" , 27017 );
m.getDB("mydb"); // where mydb is the database name
 
// Retrieve a collectionn
Mongo m = new Mongo( "localhost" , 27017 );
DB db = m.getDB("mydb");
db.getCollection("mycollection"); // where mycollection is the collection name
 
The same you can see in command prompt below.
 

Now our schema is based on Person class. Below is the class Person we like to store in the DB.
Person.java

Now "PersonService" java class to do CRUD operations.
PersonService.java

This service defines our basic CRUD system. We have the following public methods:
getAll() - for retrieving all persons
edit() - for editing
delete() - for deleting
add() - for adding
get() - for retrieving single person

We also have the following private methods:
init() - for initializing our database
getDBObject() - for retrieving a single Mongo object

The database is initialized once in the PersonService's constructor through the init() method:
private void init() {
  // Populate our MongoDB database
 
  logger.debug("Init MongoDB users");
 
  // Drop existing collection
  MongoDBFactory.getCollection("mydb","mycollection").drop();
  // Retrieve collection. If not existing, create a new one
  DBCollection coll = MongoDBFactory.getCollection("mydb","mycollection");
   
  // Create new object
  BasicDBObject doc = new BasicDBObject();
  // Generate random id using UUID type 4
        doc.put("id", UUID.randomUUID().toString());
        doc.put("firstName", "Naresh");
        doc.put("lastName", "Kancharla");
        doc.put("money", 1000);
        coll.insert(doc);
   
        // Create new object
        doc = new BasicDBObject();
  // Generate random id using UUID type 4
        doc.put("id", UUID.randomUUID().toString());
        doc.put("firstName", "Keerthi");
        doc.put("lastName", "Kancharla");
        doc.put("money", 2000);
        coll.insert(doc);
         
        // Create new object
        doc = new BasicDBObject();
  // Generate random id using UUID type 4
        doc.put("id", UUID.randomUUID().toString());
        doc.put("firstName", "Kanish");
        doc.put("lastName", "Gupta");
        doc.put("money", 3000);
        coll.insert(doc);
   
 }
 
If you Notice we're creating a dynamic JSON schema here with the following format:

   id:'',
   firstName:'',
   lastName:'',
   money:''
}
 
To run this flow we need Spring MVC Controller.

To view complete code below i provided GitHub source code link.

This controller declares the following mappings:
/persons - for retrieving all persons
/persons/add (GET) - displays the Add New form
/persons/add (POST) - saves the new person
/persons/delete - deletes an existing person
/persons/edit (GET) - displays the Edit form
/persons/edit (POST) - saves the edited person

Each mapping delegates the call to the PersonService.
When the PersonService is done processing, the controller then forwards the request to a JSP page
that displays a confirmation message. Here are the JSP pages.

personpage.jsp





editpage.jsp

addpage.jsp
 
  editedpage.jsp

addedpage.jsp
deletedpage.jsp
Spring MVC Configuration xmls are defined below.

web.xml
<servlet>
 <servlet-name>spring</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
Above web.xml we defined DispatcherServlet as "spring" so there should be a xml with name "spring-servlet.xml" for view resolution.
spring-servlet.xml

 <!-- Declare a view resolver -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/" p:suffix=".jsp" />


applicationContext.xml
<!-- Activates various annotations to be detected in bean classes -->
 <context:annotation-config />

 <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
  For example @Controller and @Service. Make sure to set the correct base-package-->
 <context:component-scan base-package="*" />

 <!-- Configures the annotation-driven Spring MVC Controller programming model.
 Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->
 <mvc:annotation-driven />

When the application is initially run, we mentioned that it will drop and create a new collection. So MongoDB's console should reflect this action as well.


Now let's access the main page that shows all registered persons. To access the main page, enter the following URL in your browser:
http://localhost:8080/SpringMongoDB/main/persons
Here's what you should see:

Here's the log from MongoDB's console: Fri Oct 26 20:32:08 [initandlisten] connection accepted from 127.0.0.1:51259 #6 (3 connections now open)

Let's edit the first record in our list by clicking the Edit link. We will be redirected to the Edit Person page:


Here's the log from MongoDB's console:
Fri Oct 26 20:34:40 [conn8] update mydb.mycollection query: { _id: ObjectId('508aa5ecad8450d889bc351a'), id: "1d11324e-ee91-4e50-84ef-b7e5e141


, firstName: "Keerthi", lastName: "K", money: 2500.0 } nscanned:1 nupdated:1 keyUpdates:0 locks(micros) w:107793 108ms

After editing the person and submitting the changes, we should see the following page:


That's it. We've managed to create a simple Spring MVC 3 application that uses MongoDB
for its database. We've also seen the benefits of a document-oriented database
when we created the schema dynamically.

Download the source code: