Introduction
Dropwizard is a lightweight RESTful Java framework. It is based on the Jetty app server, Jersey REST framework, and Jackson JSON parser. Yammer developed Dropwizard. It supports JVM(Java Virtual Machine) based backend. It provides the best Java libraries to embed in your applications. Dropwizard Views is one of the modules of Dropwizard.
In this article, we will be discussing the dropwizard views module. We will learn how to work on the database using this module.
Dropwizard Views
The dropwizard views support the localized template files. It provides us with simple and quick views of HTML, which gets done using a freemarker or mustache.
The 'io.dropwizard.views' package contains interfaces, classes and exceptions. Let us discuss these in detail.
Interface:
- ViewConfigurable<T>: It is an interface of the package.
-
ViewRenderer: It gets used to render a type of view.
Class:
-
View: Described as a view class of Dropwizard.
-
ViewBundle<T>: An application's rendering of FreeMarker & Mustache views is enabled by default with the help of ConfiguredBundle.
-
ViewMessageBodyWriter: It is a class in the package.
-
ViewRenderExceptionMapper: When a ViewRenderException is triggered, an ExtendedExceptionMapper returns a 500 error response with a generic HTML error page.
Exception:
- ViewRenderException: Implies that an error occurred while rendering a view.
Maven Dependency
The “pom.xml” file may lack the maven dependency in dropwizard-views. To avoid this, we can add the following in the “pom.xml” file:
<dependency>
<groupId>
io.dropwizard
</groupId>
<artifactId>
dropwizard-views
</artifactId>
// The ${dropwizard.version} is to mention the current version of dropwizard.
<version>
${dropwizard.version}
</version>
</dependency>
View Configuration
Let us look at the configuration of views in dropwizard.
# This is the configuration used for external views
views:
templatePaths:
# Template path for Freemarker
- /data/templates/freemarker
# Template path for Mustache
- /data/templates/mustache
# Class template path
- classpath
renderers:
# ftl is a template used by the engine to auto-generate HTML webpages
.ftl:
# Configuration of Freemarker
An array of paths to be used in template search is found in the templatePaths element. The specific classpath reference denotes that we should add the jar file to the list of search sites. We should specify that it is not assumed or implied and must be explicitly stated. Without templatePaths, we will utilize the jar file as usual.
The renderer-specific configuration is contained in the renderers element, which is provided to the relevant renderer implementation.
Now let us discuss the templates used in views.