Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Dropwizard Views
2.1.
Maven Dependency
2.2.
View Configuration
3.
Templates Supported
3.1.
Dropwizard Freemarker Template
3.2.
Dropwizard Mustache Template
4.
Template Errors
5.
Caching
6.
Custom Error Pages
7.
Frequently Asked Questions
7.1.
What is Dropwizard? 
7.2.
What is bootstrap in Dropwizard?
7.3.
What is the Freemarker template in Drowizard?
7.4.
What are the Dropwizard views?
7.5.
Name the libraries gathered in Dropwizard.
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dropwizard Views

Author Sneha Mallik
0 upvote

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. 

Introduction

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.

 Dropwizard Views

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.

Templates Supported

Dropwizard view currently supports the FreeMarker template and Mustache template. We must select one of the view templates to use Dropwizard views. 

Templates Supported

The "dropwizard-view" dependency gets replaced with "dropwizard-views-mustache" or "dropwizard-views-freemarker", and dropwizard-view will then get included as a dependency. We will add the ViewBundle to our application's initialize method to enable views.

Dropwizard Freemarker Template

The FreeMarkerTM is a template engine. It's a Java library that uses templates and data to generate text output. FreeMarker Template Language (FTL), a simple and specialized language, is used to create templates. We can concentrate on how to present the data by using a template. We will be looking at the data to present outside of the template. It is a free template that we can easily use.

Dropwizard Freemarker Template

We can use the following code for freemarker dependency.

<!--- https://mvnrepository.com/artifact/io.dropwizard/dropwizard-views-freemarker --->
<dependency>
	<groupId>
		io.dropwizard
	</groupId>
	<artifactId>
		dropwizard-views-freemarker
	</artifactId>

	// The ${dropwizard.version} is to mention the current version of dropwizard.
	<version>
		${dropwizard.version}
	</version>
</dependency>

Dropwizard Mustache Template

The Mustache is a logic-less template used in dropwizard views. Mustache is ideal for HTML, configuration files, source code, etc. It uses values from a hash or object to expand tags in a template.

Since there are no if statements, else clauses, or for loops, we call it "logic less". There are only tags present. Some tags are replaced with a value, others with no value and others with a set of values.

Dropwizard Mustache Template

Mustache template allows us to use pre-written text files containing placeholders that the system will fill in during run-time with values unique to a given request.

Template Errors

The templates use it to notify errors, such as invalid input. It helps the editor see the errors. It also helps other templates to catch errors and manage them. For example, suppose an error has occurred in a template (E.g., The template file is missing or there is an error while compiling the template). In that case, the user will receive an Internal Server Error-500 with a generic HTML message by default. Under the error mode, the precise error will get logged.

Template Errors

We will create an exception mapper that will help to override the default one by looking for ViewRenderException. The ViewRenderException will help to customize the behavior. If we cannot find mustache templates, we can return error 404 instead of an internal server error.

Caching

The templates get cached by default to improve the loading time. 

Caching

We will set the cache property in the view configuration. We keep it as false to disable it during development mode.

views:
	.mustache
	cache: false 

Custom Error Pages

You can use a custom error view to get HTML error pages that match the application. We will create a View with an ErrorMessage parameter in its constructor. Then we will connect it by registering an ErrorEntityWriter instance.

Custom Error Pages

We can register another ErrorEntityWriter that handles ValidationErrorMessage objects to handle validation error messages.

Frequently Asked Questions

What is Dropwizard? 

Dropwizard is an open-source framework of Java. It is used for the fast development of high-performance RESTful web services. 

What is bootstrap in Dropwizard?

The bootstrap is a pre-start (temporary) application environment. It contains all of the components needed to bootstrap a Dropwizard command.

What is the Freemarker template in Drowizard?

The FreeMarkerTM is a template engine. It's a Java library that uses templates and data to generate text output.

What are the Dropwizard views?

The dropwizard views support the localized template files. It provides us with simple and quick views of HTML, which is done by using a freemarker or mustache.

Name the libraries gathered in Dropwizard.

Dropwizard gathers many popular libraries. These are Jersey, Jackson, Jetty, JUnit, Guava, etc. Furthermore, it uses its own library called Metrics.

Conclusion

In this article, we covered all about the Dropwizard views. The reader can carry out a thorough understanding of the topic by referring to the Official Documentation.

Take a look at similar topics and check out our related articles-

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, JavaScript, System Design, DBMS, Java, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations. 

Happy Learning, Ninja!🥷✨

Live masterclass