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.
[sourcecode language=”xml”]
<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>
[/sourcecode]
Now create a separate resources folder and add the hello.ftl template file inside it.
[sourcecode language=”html”]
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Hello ${name}</h1>
</body>
</html>
[/sourcecode]
Spark is mini java framework which is run on jetty server.
Now add the following java class.
[sourcecode language=”java”]
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;
}
});
}
}
[/sourcecode]
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”} :)