Table of contents
1.
Introduction
2.
About Dropwizard
3.
Configuration of Dropwizard
4.
Environment Variables
5.
SSL (Secure Sockets Layer)
6.
Frequently Asked Questions
6.1.
Does Dropwizard use Log4j?
6.2.
What are Dropwizard bundles?
6.3.
Is Dropwizard a framework?
6.4.
Who developed Dropwizard?
6.5.
Which server does Dropwizard use?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Dropwizard-Configuration

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Have you ever created RESTful web services? Have to try that using any Java framework?

dropwizard

This article is focused on one of the frameworks of Java, which is known as Dropwizard. We will also study the Configuration of Dropwizard. Let's see all these things in more detail.

About Dropwizard

dropwizard

Dropwizard is an open-source Java framework. It is used to create high-performance RESTful web services quickly. It contains a few well-known libraries to make the lightweight package. It primarily uses the Jetty, Jackson, Jersey, JUnit, and Guava libraries. It also makes use of its own Metrics library.

Configuration of Dropwizard

config

Dropwizard offers several built-in configuration parameters. There is just one type parameter for each Application subclass, corresponding to the subclass of Configuration. These are at the root of the main package of your Application. For instance, the User application would have two classes: UserApplicationConfiguration, which extends Configuration, and UserApplication, which extends Application<UserApplicationConfiguration>.

Dropwizard parses the specified YAML configuration file. Then it creates an instance of your Application's configuration class by mapping YAML field names to object field names when your Application executes Configured Commands like the server command.

We advise classifying related configuration parameters into separate configuration classes to keep your configuration file and class manageable. 

We advise that you construct a separate MessageQueueFactory class if, for instance, your Application needs a specific set of configuration settings to connect to a message queue.

public class MessageQueueFactory {
    @NotEmpty
    private String host;


    @Min(1)
    @Max(65535)
    private int port = 5672;


    @JsonProperty
    public String getHost() {
        return host;
    }


    @JsonProperty
    public void setHost(String host) {
        this.host = host;
    }


    @JsonProperty
    public int getPort() {
        return port;
    }


    @JsonProperty
    public void setPort(int port) {
        this.port = port;
    }


    public MessageQueueClient build(Environment environment) {
        MessageQueueClient testclient = new MessageQueueClient(getHost(), getPort());
        environment.lifecycle().manage(new Managed() {
            @Override
            public void stop() {
                testclient.close();
            }
        });
        return testclient;
    }
}
You can also try this code with Online Java Compiler
Run Code

In the above example, the factory will automatically bind the MessageQueueClient connection to the lifecycle of the environment of the Application.

Now, your main subclass, i.e., Configuration, can include this as a member field:

public class ExampleConfiguration extends Configuration {
    @Valid
    @NotNull
    private MessageQueueFactory messageQueue = new MessageQueueFactory();


    @JsonProperty("messageQueue")
    public MessageQueueFactory getMessageQueueFactory() {
        return messageQueue;
    }


    @JsonProperty("messageQueue")
    public void setMessageQueueFactory(MessageQueueFactory factory) {
        this.messageQueue = factory;
    }
}
You can also try this code with Online Java Compiler
Run Code

And now, the other subclass, i.e., the Application, can utilize the factory directly to build a client for the message queue.

public void run(ExampleConfiguration configuration,
                Environment environment) {
    MessageQueueClient messageQueue = configuration.getMessageQueueFactory().build(environment);
}
You can also try this code with Online Java Compiler
Run Code

After that, you can utilize a nested messageQueue in the YAML file of the Application. 

messageQueue:
  host: mq.example.com
  port: 5673
You can also try this code with Online Java Compiler
Run Code

The Dropwizard's Validation feature includes the annotations @NotNull, @NotEmpty, @Min, @Max, and @Valid.

Dropwizard would not launch if the messageQueue.host entry in your YAML configuration file was empty or missing, and it would also print an error message outlining the problems.

Dropwizard uses your Application subclass to initialize your Application's Environment after your Application has processed the YAML file and built its Configuration instance.

Environment Variables

code

The EnvironmentVariableSubstitutor and SubstitutingSourceProvider features of the dropwizard-configuration module enable users to replace configuration parameters with the values of environment variables.

public class MyApplication extends Application <MyConfiguration> {
    // [...]
    @Override
    public void initialize(Bootstrap<MyConfiguration> bootstrap) {
        // Enable variable substitution with environment variables
        bootstrap.setConfigurationSourceProvider(
                new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(),
                                                   new EnvironmentVariableSubstitutor(false)
                )
        );


    }


    // [...]
}
You can also try this code with Online Java Compiler
Run Code

The configuration values that need to be replaced must be specified clearly in the configuration file and adhere to the StringSubstitutor substitution criteria from the Apache Commons Text library.

mySetting: ${DW_MY_SETTING}
defaultSetting: ${DW_DEFAULT_SETTING:-default value}
You can also try this code with Online Java Compiler
Run Code

SubstitutingSourceProvider can replace configuration source variables with arbitrary values by passing a custom StringSubstitutor implementation rather than being constrained to substituting environment variables.

SSL (Secure Sockets Layer)

ssl

Dropwizard includes built-in SSL support. You will have to create your own Java keystore, which is outside the document's scope (Jetty's documentation can help you get started with keytool). You can utilize a test keystore in the Dropwizard sample project.

Server:
  applicationConnectors:
    - type: https
      port: 8443
      keyStorePath: example.keystore
      keyStorePassword: example
      validateCerts: false
You can also try this code with Online Java Compiler
Run Code

Only secure TLSv1.2 cipher suites are permitted by default. It's possible that older versions of cURL, Java versions 6 and 7, and other clients cannot communicate with the permitted cipher suites. Still, this deliberate choice neglected interoperability in exchange for security.

Dropwizard offers a solution by allowing the specification of a personalized set of encryption suites. The JVM defaults are used if no supported protocol lists or cipher suites are given. The defaults are inherited from Jetty if no lists of prohibited protocols or cipher suites are supplied.

Clients using TLSv1 and TLSv1.1 can negotiate a connection identical to pre-Dropwizard 1.0 by using the following list of prohibited cipher suites.

server:
  applicationConnectors:
    - type: https
      port: 8443
      excludedCipherSuites:
        - SSL_RSA_WITH_DES_CBC_SHA
        - SSL_DHE_RSA_WITH_DES_CBC_SHA
        - SSL_DHE_DSS_WITH_DES_CBC_SHA
        - SSL_RSA_EXPORT_WITH_RC4_40_MD5
        - SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
        - SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
        - SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
You can also try this code with Online Java Compiler
Run Code

Since version 9.4.8 (Dropwizard 1.2.3), Jetty has supported native SSL with Google's Conscrypt, which manages cryptography using BoringSSL (Google's fork of OpenSSL). You may activate it in Dropwizard by adding the provider to your app's registration:

<dependency>
    <groupId>org.conscrypt</groupId>
    <artifactId>conscrypt-openjdk-uber</artifactId>
    <version>${conscrypt.version}</version>
</dependency>

 

static {
    Security.insertProviderAt(new OpenSSLProvider(), 1);
}
You can also try this code with Online Java Compiler
Run Code

You can set the JCE provider in the Configuration by:

server:
  type: simple
  connector:
    type: https
    jceProvider: Conscrypt
You can also try this code with Online Java Compiler
Run Code

You must add an ALPN Conscrypt provider as a dependency for HTTP/2 servers.

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-alpn-conscrypt-server</artifactId>
</dependency>
You can also try this code with Online Java Compiler
Run Code

Frequently Asked Questions

Does Dropwizard use Log4j?

Dropwizard makes use of Logback for its logging backend. It also provides an slf4j implementation. It even routes all Log4j, Java.util.logging, and Apache Commons Logging usage through Logback.

What are Dropwizard bundles?

A Dropwizard Bundle is a reusable collection of functionality. The Dropwizard project itself sometimes provides these functionalities. The purpose of these functionalities is to define blocks of an application's behavior.

Is Dropwizard a framework?

Yes, Dropwizard is an open-source Java framework. The purpose is to develop ops-friendly, high-performance RESTful backends.

Who developed Dropwizard?

Yammer developed Dropwizard to power their JVM-based backend.

Which server does Dropwizard use?

Dropwizard utilizes the Jetty HTTP library for embedding a tuned HTTP server directly into your project.

Conclusion

In this article, we have studied one of the Java frameworks, which is known as Dropwizard, in detail.

We hope that this article has provided you with the help to enhance your knowledge regarding Dropwizard and if you would like to learn more, check out our articles on Dropwizard JDBI3, and Dropwizard Client.  

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, 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.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass