VOOZH about

URL: https://www.baeldung.com/java-dropwizard

⇱ Introduction to Dropwizard | Baeldung


πŸ‘ Image
eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
πŸ‘ announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
πŸ‘ announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
πŸ‘ announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
πŸ‘ announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
πŸ‘ announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
eBook – HTTP Client – NPI EA (cat=Http Client-Side)
πŸ‘ announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
πŸ‘ announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
πŸ‘ announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
πŸ‘ announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
πŸ‘ announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
πŸ‘ announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New β€œREST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
πŸ‘ announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
πŸ‘ announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
πŸ‘ announcement - icon

Refactor Java code safely β€” and automatically β€” with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions β€” one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
πŸ‘ announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Overview

Dropwizard is an open-source Java framework used for the fast development of high-performance RESTful web services. It gathers some popular libraries to create the lightweight package. The main libraries that it uses are Jetty, Jersey, Jackson, JUnit, and Guava. Furthermore, it uses its own library called Metrics.

In this tutorial, we’ll learn how to configure and run a simple Dropwizard application. When we’re done, our application will expose a RESTful API that allows us to obtain a list of stored brands.

2. Maven Dependencies

Firstly, the dropwizard-core dependency is all we need in order to create our service. Let’s add it to our pom.xml:

<dependency>
 <groupId>io.dropwizard</groupId>
 <artifactId>dropwizard-core</artifactId>
 <version>2.0.0</version>
</dependency>

3. Configuration

Now, we’ll create the necessary classes that are needed for every Dropwizard application to run.

Dropwizard applications store properties in YML files. Therefore, we’ll create the introduction-config.yml file in the resource directory:

defaultSize: 5

We can access values in that file by creating a class that extends io.dropwizard.Configuration:

public class BasicConfiguration extends Configuration {
 @NotNull private final int defaultSize;

 @JsonCreator
 public BasicConfiguration(@JsonProperty("defaultSize") int defaultSize) {
 this.defaultSize = defaultSize;
 }

 public int getDefaultSize() {
 return defaultSize;
 }
}

Dropwizard uses Jackson to deserialize the configuration file into our class. Hence, we’ve used Jackson’s annotations.

Next, let’s create the main application class, which is responsible for preparing our service for usage:

public class IntroductionApplication extends Application<BasicConfiguration> {

 public static void main(String[] args) throws Exception {
 new IntroductionApplication().run("server", "introduction-config.yml");
 }

 @Override
 public void run(BasicConfiguration basicConfiguration, Environment environment) {
 //register classes
 }

 @Override
 public void initialize(Bootstrap<BasicConfiguration> bootstrap) {
 bootstrap.setConfigurationSourceProvider(new ResourceConfigurationSourceProvider());
 super.initialize(bootstrap);
 }
}

Firstly, the main method is responsible for running the application. We could either pass the args to the run method or fill it by ourselves.

The first argument can be either server or check. The check option validates the configuration, while the server option runs the application. The second argument is the location of the configuration file. 

Furthermore, the initialize method sets the configuration provider to the ResourceConfigurationSourceProvider, which allows the application to find a given configuration file in the resource directory. It isn’t obligatory to override this method.

Lastly, the run method allows us to access both the Environment and the BaseConfiguration, which we’ll use later in this article.

4. Resource

Firstly, let’s create a domain class for our brand:

public class Brand {
 private final Long id;
 private final String name;

 // all args constructor and getters
}

Secondly, let’s create a BrandRepository class that’ll be responsible for returning brands:

public class BrandRepository {
 private final List<Brand> brands;

 public BrandRepository(List<Brand> brands) {
 this.brands = ImmutableList.copyOf(brands);
 }

 public List<Brand> findAll(int size) {
 return brands.stream()
 .limit(size)
 .collect(Collectors.toList());
 }

 public Optional<Brand> findById(Long id) {
 return brands.stream()
 .filter(brand -> brand.getId().equals(id))
 .findFirst();
 }
}

Additionally, we were able to use the ImmutableList from Guava because it’s part of Dropwizard itself.

Thirdly, we’ll create a BrandResource class. The Dropwizard uses JAX-RS by default with Jersey as implementation. Therefore, we’ll make use of annotations from this specification to expose our REST API endpoints:

@Path("/brands")
@Produces(MediaType.APPLICATION_JSON)
public class BrandResource {
 private final int defaultSize;
 private final BrandRepository brandRepository;

 public BrandResource(int defaultSize, BrandRepository brandRepository) {
 this.defaultSize = defaultSize;
 this.brandRepository = brandRepository;
 }

 @GET
 public List<Brand> getBrands(@QueryParam("size") Optional<Integer> size) {
 return brandRepository.findAll(size.orElse(defaultSize));
 }

 @GET
 @Path("/{id}")
 public Brand getById(@PathParam("id") Long id) {
 return brandRepository
 .findById(id)
 .orElseThrow(RuntimeException::new);
 }
}

Additionally, we’ve defined size as Optional in order to use defaultSize from our configuration if the argument is not provided.

Lastly, we’ll register BrandResource in the IntroductionApplicaton class. In order to do that, let’s implement the run method:

@Override
public void run(BasicConfiguration basicConfiguration, Environment environment) {
 int defaultSize = basicConfiguration.getDefaultSize();
 BrandRepository brandRepository = new BrandRepository(initBrands());
 BrandResource brandResource = new BrandResource(defaultSize, brandRepository);

 environment
 .jersey()
 .register(brandResource);
}

All created resources should be registered in this method.

5. Running Application

In this section, we’ll learn how to run the application from the command line.

First, we’ll configure our project to build a JAR file using the maven-shade-plugin:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-shade-plugin</artifactId>
 <configuration>
 <createDependencyReducedPom>true</createDependencyReducedPom>
 <filters>
 <filter>
 <artifact>*:*</artifact>
 <excludes>
 <exclude>META-INF/*.SF</exclude>
 <exclude>META-INF/*.DSA</exclude>
 <exclude>META-INF/*.RSA</exclude>
 </excludes>
 </filter>
 </filters>
 </configuration>
 <executions>
 <execution>
 <phase>package</phase>
 <goals>
 <goal>shade</goal>
 </goals>
 <configuration>
 <transformers>
 <transformer
 implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
 <mainClass>com.baeldung.dropwizard.introduction.IntroductionApplication</mainClass>
 </transformer>
 </transformers>
 </configuration>
 </execution>
 </executions>
</plugin>

This is the suggested configuration of the plugin. Additionally, we’ve included the path to our main class in the <mainClass> element.

Finally, we’ll build the application with Maven. Once we have our JAR file, we can run the application:

java -jar target/dropwizard-0.0.1-SNAPSHOT.jar

There’s no need to pass the parameters because we’ve already included them in the IntroductionApplication class.

After that, the console log should end with:

INFO [2020-01-08 18:55:06,527] org.eclipse.jetty.server.Server: Started @1672ms

Now, the application is listening on port 8080, and we can access our brand endpoint at http://localhost:8080/brands.

6. Changing the Default Port

By default, a typical Dropwizard application uses two ports:

  • Port 8080 for the main application
  • Port 8081 for the administration interface

However, we can change the default ports using either the configuration folder or the command-line arguments.

6.1. Changing Port Through the Configuration File

To change the default port through the configuration file, we need to modify the introduction-config.yml in the resource directory:

server:
 applicationConnectors:
 - type: http
 port: 9090
 adminConnectors:
 - type: http
 port: 9091

This configuration sets the application to run on port 9090 and the admin interface on port 9091. We can apply this configuration by passing the configuration file to the run() method:

// ...
new IntroductionApplication().run("server", "introduction-config.yml");
// ...

Also, we can apply the configuration file through the command line when starting the application:

$ java -jar target/dropwizard-0.0.1-SNAPSHOT.jar server introduction-config.yml

In the command above, we start the application with the server and introduction-config.yml arguments.

6.2. Changing Port Through the Command Line

Alternatively, we can override the port settings using JVM system properties on the command line:

$ java -Ddw.server.applicationConnectors[0].port=9090 \ 
 -Ddw.server.adminConnectors[0].port=9091 \ 
 -jar target/dropwizard-0.0.1-SNAPSHOT.jar server

The -Ddw prefix defines Dropwizard-specific system properties that set the main application port and the administrative interface port. These system properties, if specified, override the configuration file setting. Values in the configuration file are used if no system properties are defined.

7. Health Check

When starting the application, we were informed that the application doesn’t have any health checks. Fortunately, Dropwizard provides an easy solution to add health checks to our application.

Let’s start by adding a simple class that extends com.codahale.metrics.health.HealthCheck:

public class ApplicationHealthCheck extends HealthCheck {
 @Override
 protected Result check() throws Exception {
 return Result.healthy();
 }
}

This simple method will return information about the healthiness of our component. We could create multiple health checks, and some of them might fail in certain situations. For instance, we would return Result.unhealthy() if the connection to the database failed.

Lastly, we need to register our health check in the run method of our IntroductionApplication class:

environment
 .healthChecks()
 .register("application", new ApplicationHealthCheck());

After running the application, we can check the health check response under http://localhost:8081/healthcheck:

{
 "application": {
 "healthy": true,
 "duration": 0
 },
 "deadlocks": {
 "healthy": true,
 "duration": 0
 }
}

As we can see, our health check has been registered under the application tag.

8. Conclusion

In this article, we’ve learned how to set up the Dropwizard application with Maven.

We’ve discovered that the base setup of the application is really easy and fast. Additionally, Dropwizard includes every library that we need to run the high-performance RESTful web service.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
πŸ‘ announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
πŸ‘ announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
πŸ‘ announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
πŸ‘ announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
πŸ‘ announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

πŸ‘ announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
πŸ‘ announcement - icon

Modern Java teams move fast β€” but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural β€” and as fast β€” as writing code, this is a good place to start.

Course – LS – NPI (cat=REST)
πŸ‘ announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

eBook Jackson – NPI EA – 3 (cat = Jackson)