Dropwizard Tutorial : HTML Page Using Freemarker


Before you start this, Its good to follow the following articles.

Running Rest API using Dropwizard

Reading From Database Using Dropwizard 

Writing To Database Using Dropwizard

I will assume that your dropwizard project is running.


Add dependency of dropwizard-freemarker-views in your pom.xml

Your pom.xml should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!– https://mvnrepository.com/artifact/io.dropwizard/dropwizard-views-freemarker –>

view raw


hosted with ❤ by GitHub

Freemarker Template Language

Apache FreeMarker™ is a template engine. It’s a Java library to generate text output based on templates and data. Templates are written in the FreeMarker Template Language (FTL), which is a simple and specialized language. Template allows us to focus on how to present the data. Outside the template you are focusing on what data to present.

Further reading here.

Template Configuration Context

import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;
import java.io.File;
import java.io.IOException;
* Created by harshvardhan on 19/07/18.
public class TemplateConfigurationContext {
private static Configuration configuration = new Configuration(Configuration.VERSION_2_3_27);
public static Configuration getConfiguration() {
try {
configuration.setDirectoryForTemplateLoading(new File("templates"));
} catch (IOException e) {
return configuration;

Hello World FTL

Add a directory at src level and add helloWorld.ftl

<h1>Welcome ${user}!</h1>

view raw


hosted with ❤ by GitHub

Hello World GET API

Add a get API to render HelloWorld FTL in browser

import com.codahale.metrics.annotation.Timed;
import freemarker.template.Template;
import io.dropwizard.hibernate.UnitOfWork;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
* Created by harshvardhan on 12/07/18.
public class MyResource {
private InfoDao infoDao;
public MyResource(InfoDao infoDao) {
this.infoDao = infoDao;
public String getName() {
return "Harsh";
public String postName(String name) {
System.out.println("Name given by : "+name);
return "Ok";
public List<Info> findAllEmp() {
return infoDao.findAll();
public String saveEmp(Info info) {
return infoDao.create(info);
public Response getHelloWorld() {
try {
Template temp = TemplateConfigurationContext.getConfiguration().getTemplate("helloWorld.ftl");
Map root = new HashMap();
root.put("user", "Harsh");
Writer writer = new StringWriter();
temp.process(root, writer);
return Response.status(Response.Status.ACCEPTED).entity((writer.toString())).build();
} catch (Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(("Oops! Try again later")).build();

view raw


hosted with ❤ by GitHub

Run The Project

Open the API in the browser to see your helloWorld rendered.

Screen Shot 2018-07-19 at 3.30.13 PM.png

Further Reading:



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 )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: