Getting Started
Dropwizard doesn’t have any fancy project-initialization helpers or Maven plug-ins. Getting started with Dropwizard follows a similar pattern to any plain-old Java project: use a Maven archetype, or add it to an existing application with whatever tools you currently use. You could also use JBoss Forge, which is a technology-agnostic Java project coordination and management tool that allows you to quickly create projects, add dependencies, add classes, etc. For this section, we’ll just use Maven archetypes.
Choose a directory where you want to create your new Dropwizard project. Also verify you have Maven installed. You can run the following command from your operating system’s command prompt, or you can use the following information in the following command to populate a dialog box or wizard for your favorite IDE:
$ mvn -B archetype:generate \
-DarchetypeGroupId=io.dropwizard.archetypes \ -DarchetypeArtifactId=java-simple -DarchetypeVersion=0.9.2 \
-DgroupId=com.redhat.examples.dropwizard
-DartifactId=hola-dropwizard -Dversion=1.0 -Dname=HolaDropwizard Navigate to the directory that the Maven archetype generator created for us in hola-dropwizard and run the following command to build our project:
$ mvn clean install
You should have a successful build!
This uses the Dropwizard archetype java-simple to create our microservice. If you go into the hola-dropwizard directory, you should see this structure:
./src/main/java/com/redhat/examples/dropwizard/api
./src/main/java/com/redhat/examples/dropwizard/cli
./src/main/java/com/redhat/examples/dropwizard/client
./src/main/java/com/redhat/examples/dropwizard/core
./src/main/java/com/redhat/examples/dropwizard/db
./src/main/java/com/redhat/examples/dropwizard/health
./src/main/java/com/redhat/examples/dropwizard /HolaDropwizardApplication.java
./src/main/java/com/redhat/examples/dropwizard
/HolaDropwizardConfiguration.java
./src/main/java/com/redhat/examples/dropwizard/resources
./src/main/resources
./src/main/resources/assets
./src/main/resources/banner.txt
./pom.xml
Note that Dropwizard creates a package structure for you that follows their convention: api
POJOs that define the objects used in your REST resources (some people call these objects domain objects or DTOs). cli
This is where your Dropwizard commands go (additional commands you wish to add to startup of your application). client
Client helper classes go here. db
Any DB related code or configuration goes here. health
Microservice-specific health checking that can be exposed at runtime in the admin interface goes here. resources
Our REST resource classes go here.
We also have the files HolaDropwizardApplication.java and HolaDropwizardConfiguration.java, which is where our configuration and bootstrapping code goes. Take a look at the HolaDropwizardAp plication class in Example 3-1, for example.
Example 3-1. src/main/java/com/redhat/examples/dropwizard/ HolaDropwizardApplication.java
public class HolaDropwizardApplication extends Application<HolaDropwizardConfiguration> {
public static void main(final String[] args)
throws Exception {
new HolaDropwizardApplication().run(args); }
Getting Started
@Override public String getName() { return "HolaDropwizard"; }
@Override
public void initialize( Bootstrap<HolaDropwizardConfiguration> bootstrap) {
// TODO: application initialization
}
@Override
public void run(HolaDropwizardConfiguration configuration, final Environment environment) {
// TODO: implement application
}
}
This class contains our public static void main() method, which doesn’t do too much except call our microservice’s run() method. It also has a getName() method, which is shown at startup. The initialize() and run() methods are the key places where we can bootstrap our application as we’ll show in the next section.
The configuration class that was generated, HolaDropwizardConfigu ration, is empty for now (Example 3-2).
Example 3-2. src/main/java/com/redhat/examples/dropwizard/ HolaDropwizardConfiguration.java
public class HolaDropwizardConfiguration
extends Configuration {
// TODO: implement service configuration
}
Although Dropwizard doesn’t have any special Maven plug-ins on its own, take a look at the pom.xml that was generated. We see that the Dropwizard dependencies are on the classpath and that we’ll be using the maven-shade-plugin to package up our JAR as an uber JAR. This means all of our project’s dependencies will be unpacked (i.e., all dependency JARs unpacked) and combined into a single JAR that our build will create. For that JAR, we use the maven-jarplugin to make it executable.
One plug-in we do want to add is the exec-maven-plugin. With Spring Boot we were able to just run our microservice with mvn spring-boot:run. We want to be able to do the same thing with our Dropwizard application, so let’s add the following plug-in within the <build> section of the pom.xml, shown in Example 3-3.
Example 3-3. pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId>
<configuration> <mainClass>
com.redhat.examples.dropwizard.HolaDropwizardApplication
</mainClass>
<arguments>
<argument>server</argument>
</arguments>
</configuration>
</plugin>
Now we can execute our application from the command line like this:
$ mvn exec:java
We should see something like what’s in Example 3-4.
Example 3-4. HolaDropwizard
====================================================================
HolaDropwizard
====================================================================
INFO [2016-03-27 21:54:22,279] io.dropwizard.server.DefaultServer...
: Registering jersey handler with root path prefix: / INFO [2016-03-27 21:54:22,291] io.dropwizard.server.DefaultServer...
: Registering admin handler with root path prefix: / INFO [2016-03-27 21:54:22,326] org.eclipse.jetty.setuid.SetUIDL...
: Opened application@5dee571c{HTTP/1.1}{0.0.0.0:8080} INFO [2016-03-27 21:54:22,327] org.eclipse.jetty.setuid.SetUIDL...
: Opened admin@f8bd099{HTTP/1.1}{0.0.0.0:8081}
INFO [2016-03-27 21:54:22,329] org.eclipse.jetty.server.Server
: jetty-9.2.13.v20150730 INFO [2016-03-27 21:54:22] io.dropwizard.jersey.DropwizardResou...
: The following paths were found for the configured resources:
Getting Started
NONE
INFO [2016-03-27 21:54] org.eclipse.jetty.server.handler.Context...
: Started i.d.j.MutableServletContextHandler@1dfb9685{/,null,AVAI...
INFO [2016-03-27 21:54:22] io.dropwizard.setup.AdminEnvironment:...
POST /tasks/log-level (dropwizard.servlets.tasks.LogConfigurat...
POST /tasks/gc (io.dropwizard.servlets.tasks.GarbageCollection...
WARN [2016-03-27 21:54:22,695] io.dropwizard.setup.AdminEnvironm...
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!THIS APPLICATION HAS NO HEALTHCHECKS. THIS MEANS YOU WILL NEVER...
! IF IT DIES IN PRODUCTION, WHICH MEANS YOU WILL NEVER KNOW IF...
!LETTING YOUR USERS DOWN. YOU SHOULD ADD A HEALTHCHECK FOR EACH...
! APPLICATION'S DEPENDENCIES WHICH FULLY (BUT LIGHTLY) TESTS...
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
INFO [2016-03-27 21:54] org.eclipse.jetty.server.handler.ContextH...
: Started i.d.j.MutableServletContextHandler@4969dc6{/,null,AVA...
INFO [2016-03-27 21:54:22,704] org.eclipse.jetty.server.ServerCo...
: Started application@5dee571c{HTTP/1.1}{0.0.0.0:8080} INFO [2016-03-27 21:54:22,705] org.eclipse.jetty.server.ServerCo...
: Started admin@f8bd099{HTTP/1.1}{0.0.0.0:8081}
INFO [2016-03-27 21:54:22,705] org.eclipse.jetty.server.Server
: Started @2914ms
If you see the application start up, you can try to navigate in your browser to the default location for RESTful endpoints: http://local host:8080. You probably won’t see too much:
{"code":404,"message":"HTTP 404 Not Found"}
If you try going to the admin endpoint http://localhost:8081, you should see a simple page with a few links. Try clicking around to see what kind of value is already provided out of the box for managing your microservice!