DropWizard Framework in JAVA

by | Dec 17, 2020 | Java

What is the DropWizard Framework in JAVA?

If you are working with REST (Representational State Transfer) APIs, the DropWizard framework of JAVA is the one to use. It is directly responsible for the development of Restful APIs. The framework is both lightweight and open source. The Drop Wizard framework has all of the following dependencies included in its package: Jersey, Jackson, and Jetty. The main goal of the DropWizard framework, like that of other frameworks in JAVA, is to enable the construction of web-applications that are robust in structure, efficient in performance and has reliable implementations in as less time as possible time.

Libraries in the DropWizard Framework

  • Jersey – One of the main building blocks of web-based applications.
  • Jetty – The Jetty HTTP library facilitates the addition of an HTTP server to your application.
  • FreeMarker – This directly affects the view component by developing display templates.
  • Joda Time – This library is used to manage problems with date and time.
  • JDBI – JDBI (Java Database Interface) handles all communicates to and from the Java Database.

Advantages of DropWizard

There are several advantages to using the DropWizard. The framework is majorly used to develop high-performing and long-lasting web applications. With proper debugging done, it is quite unlikely for the application to crash any time soon. Since rapid prototyping can be carried out, the Drop Wizard has become even more popular among developers. The framework also includes several independent libraries and packages. Even third-party frameworks can be integrated into the core structure of DropWizard.

The Drop Wizard Architecture (As used in microservices)

The system architecture for Drop Wizard replicates that of the Spring framework and can be broken down into three major components. They are as follows:

  • Order/Payment Module – This module can be treated as the brain of the architecture. It is responsible for dictating workflow and also has external communications with clients who send their requests to this module.
  • PPA (Payment Provider Adapter) – These adapters directly handle user requests that they receive from the Order Module.
  • Callback server – This is the final component in the structure that prepares a response and sends it back to the order module to be presented to the user.

Example

/* In this example, we will be building a simple application that will print out ‘Test Successful’ on a web browser when it is run. For the process, we will start by building the Application Class. */
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import com.example.helloworld.resources.HelloWorldResource;
import com.example.helloworld.health.TemplateHealtCheck;
public class Sample extends Application <HelloWorldConfiguration> {
public static void main (String args []) throws Exception {
new Sample().run(args);
}
@override
public String getName() {
return “Test Successful”;
}
@override
public void initialize (BootStrap<HelloWorldConfiguration> bootstrap) {
}
@override
public void run (Sample conf, Environment env) {
}
}
//The following is the Representation class (can be thought of the view class)
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.Length;
public class Saying {
private long id;
private String content;
public Saying () {
}
public Saying (long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
//Finally, we will create the Resource Class
import java.ws.rs.GET;
import java.ws.rs.Path;
import java.ws.rs.Produces;
import java.ws.rs.QueryParam;
import java.ws.rs.core.MediaType;
import java.util.concurrent.atomic.AtomicLong;
import java.util.Optional;
public class Sampleresource {
private final String template;
private final String defaultName;
private final AtomicLong counter;
public SampleResource (String template. String defaultName) {
this.template = template;
this.defaultName = defaultName;
this.counter = new AtomicLong();
}
public Saying sayHello(@QueryParam(“name”) Optional <String> name) {
final String value = String.format(template, name.orElse (defaultName));
return new Saying (counter.incrementAndGet(), value) ;
}
}
//The following classes are only part of the application. Other important components of the code //including testing thoroughly the program as well as registering the resources successfully.

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.