Home > Java, Jetty, mongodb, NOSQL > java web application with spark + freemarker + mongodb

java web application with spark + freemarker + mongodb

Technologies used.

  •  Maven
  • Spark framework
  • Freemarker template engine
  • Java 1.7
  • MongoDB

First create a project template using maven. This project is the one i am following in the Mongodb for Java developers course conducted by 10gen.

After creating the project, edit the pom.xml as follows.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

<groupId>10gen</groupId>
 <artifactId>M101J</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>

<name>M101J</name>
 <url>http://maven.apache.org</url>

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <repositories>
 <repository>
 <id>Spark repository</id>
 <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
 </repositories>
 <dependencies>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>com.sparkjava</groupId>
 <artifactId>spark-core</artifactId>
 <version>0.9.9.7-SNAPSHOT</version>
</dependency>
<dependency>
 <groupId>org.freemarker</groupId>
 <artifactId>freemarker</artifactId>
 <version>2.3.19</version>
</dependency>
 <dependency>
 <groupId>org.mongodb</groupId>
 <artifactId>mongo-java-driver</artifactId>
 <version>2.2</version>
</dependency>

 </dependencies>
</project>

Now create a separate resources folder and add the hello.ftl template file inside it.


<html>
<head>
 <title>Welcome!</title>
 </head>
<body>
<h1>Hello ${name}</h1>
</body>
</html>

Spark is mini java framework which is run on jetty server.

Now add the following java class.


package tengen;

import java.io.StringWriter;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.ServerAddress;

import spark.Request;
import spark.Response;
import spark.Route;
import spark.Spark;
import freemarker.template.Configuration;
import freemarker.template.Template;

public class HelloWorldMongoDBfreemarkerStyle {

/**
 * @param args
 * @throws UnknownHostException
 * @throws MongoException
 */
 public static void main(String[] args) throws MongoException, UnknownHostException {
 // TODO Auto-generated method stub

final Configuration configuration = new Configuration();
 configuration.setClassForTemplateLoading(HelloWorldfreemarkerstyle.class, "/");
 Mongo client = new Mongo(new ServerAddress("localhost",27017));

 DB database = client.getDB("foo");
 final DBCollection collection = database.getCollection("foo");
 Spark.get(new Route("/") {

 @Override
 public Object handle(final Request request, final Response response) {
 StringWriter writer = new StringWriter();
 try {
 // TODO Auto-generated method stub
 Template helloTemplate = configuration.getTemplate("hello.ftl");

 DBObject document = collection.findOne();
 helloTemplate.process(document, writer);

 System.out.println(writer);
 } catch (Exception e) {
 // TODO: handle exception
 e.printStackTrace();
 }

 return writer;
 }
 });
 }

}

Now run the mongodb server. Then run the above java file. Go to localhost:4567 address in your browser. You will see “Hello MongoDB”.

PS.

there should be a collection named “foo” and inside that the following document.

{“name”:”MongoDB”} 🙂

Advertisements
Categories: Java, Jetty, mongodb, NOSQL
  1. May 29, 2013 at 1:55 pm

    You need to give the credits to 10gen MOOC course “M101J MongoDB for Java Developers”

    • supun dharmarathne
      May 29, 2013 at 5:34 pm

      FYI, i have already given the credits …

  2. ken
    August 6, 2015 at 10:37 am

    Thanks for sharing.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: