Dropwizard: Resource

This entry is part 4 of 5 in the series Dropwizard

In this tutorial I will give a basic example of a resource endpoint. If you haven’t configured Guice yet please do so before continuing.

So basically now that you have Guice configured and working you can now create an api endpoint. For this we will just use a GET but you can also do POST, PUT, DELETE.

  1. package ca.gaudreault.mydropwizardapp.resources;
  2.  
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7.  
  8. import com.codahale.metrics.annotation.Timed;
  9. import com.google.inject.Inject;
  10.  
  11. import ca.gaudraeult.mydropwizardapp.services.MyService;
  12. import ca.gaudreault.mydropwizardapp.models.MyModel;
  13.  
  14. @Timed
  15. @Path("/my-resource")
  16. public class MyResource {
  17. MyService myService;
  18.  
  19. @Inject
  20. public MyResource(final MyService myService) {
  21. this.myService = myService;
  22. }
  23.  
  24. @GET
  25. @Timed
  26. @Produces(MediaType.APPLICATION_JSON)
  27. public MyModel runTest() {
  28. return this.myService.runTest();
  29. }
  30. }

Once you run your application you can view the endpoint by going to http://localhost:8080/my-resource.

The output will be as follows.

  1. {"value":123123}

If you noticed we added the “@Timed” annotation. You can now go to http://localhost:8081/metrics?pretty=true to view the metrics on our “runTest” method. The output will look like the below.

  1. {
  2. "ca.gaudreault.mydropwizardapp.resources.MyResource.runTest": {
  3. "count": 0,
  4. "max": 0.0,
  5. "mean": 0.0,
  6. "min": 0.0,
  7. "p50": 0.0,
  8. "p75": 0.0,
  9. "p95": 0.0,
  10. "p98": 0.0,
  11. "p99": 0.0,
  12. "p999": 0.0,
  13. "stddev": 0.0,
  14. "m15_rate": 0.0,
  15. "m1_rate": 0.0,
  16. "m5_rate": 0.0,
  17. "mean_rate": 0.0,
  18. "duration_units": "seconds",
  19. "rate_units": "calls/second"
  20. }

Dropwizard: Command

This entry is part 3 of 5 in the series Dropwizard

In this tutorial I will give a brief demonstration on how to write a custom dropwizard command.

MyCommand

So below you will see the command class and how we are creating and registering a command line param called “test” which is a Boolean.

  1. package ca.gaudreault.mydropwizardapp;
  2.  
  3. import io.dropwizard.cli.Command;
  4. import io.dropwizard.setup.Bootstrap;
  5. import net.sourceforge.argparse4j.inf.Namespace;
  6. import net.sourceforge.argparse4j.inf.Subparser;
  7.  
  8. public class MyCommand extends Command {
  9.  
  10. protected MyCommand() {
  11. super("myCommand", "This is a sample command");
  12. }
  13.  
  14. @Override
  15. public void configure(Subparser subparser) {
  16. subparser.addArgument("-test").required(true).type(Boolean.class).dest("test").help("Does something really awesome");
  17. }
  18.  
  19. @Override
  20. public void run(Bootstrap<?> bootstrap, Namespace namespace) throws Exception {
  21. System.out.println("MyCommand " + namespace.getBoolean("test"));
  22. }
  23. }

MyDropwizardAppApplication

If you remember from part 1 of this series you created the based Dropwizard app. So you should have a class called “MyDropwizardAppApplication”. Open that now and modify the “initialize” like the below. Note that we are only adding the “addCommand”.

  1. @Override
  2. public void initialize(final Bootstrap bootstrap) {
  3. bootstrap.addCommand(new MyCommand());
  4. }

Executing Command

Basically now we can just call our JAR file and pass the following arguments to it.

  1. myCommand -test false

You will see once it runs that following

  1. MyCommand false

Dropwizard: Guice Bundle

This entry is part 2 of 5 in the series Dropwizard

In this tutorial I will show you how to add Guice to your Dropwizard app. This will be a very basic implementation. Some things you should note is that I didn’t put in any docstrings. You should always do that!

Now there are a few Dropwizard Guice integrations available but the most active is the one I will show you today called “dropwizard-guicey“.

POM.xml

  1. <dependency>
  2. <groupId>ru.vyarus</groupId>
  3. <artifactId>dropwizard-guicey</artifactId>
  4. <version>4.1.0</version>
  5. </dependency>

Model

Now we create a model to use with our service

  1. package ca.gaudreault.mydropwizardapp.models;
  2.  
  3. import java.io.Serializable;
  4.  
  5. import javax.validation.constraints.NotNull;
  6.  
  7. public class MyModel implements Serializable {
  8. private static final long serialVersionUID = 1L;
  9. private Integer value;
  10. public Integer getValue() {
  11. return value;
  12. }
  13. public void setValue(Integer value) {
  14. this.value = value;
  15. }
  16. }

Service

Here you will create your service interface and class so that you can bind it in the guice module.

Interface

  1. package ca.gaudraeult.mydropwizardapp.services;
  2.  
  3. import ca.gaudreault.mydropwizardapp.models.MyModel;
  4.  
  5. public interface MyService {
  6. MyModel runTest();
  7. }

Implementation

  1. package ca.gaudraeult.mydropwizardapp.services;
  2.  
  3. import ca.gaudreault.mydropwizardapp.models.MyModel;
  4.  
  5. public class MyServiceImpl implements MyService {
  6. public MyServiceImpl() { }
  7.  
  8. @Override
  9. public MyModel runTest() {
  10. final MyModel myModel = new MyModel();
  11. myModel.setValue(123123);
  12. return myModel;
  13. }
  14. }

ServerModule

Now when we create our module class you can bind the interface to the implementation. Note that if your implementation does not implement the interface this will not work.

  1. package ca.gaudreault.mydropwizardapp;
  2.  
  3. import com.google.inject.AbstractModule;
  4.  
  5. import ca.gaudraeult.mydropwizardapp.services.MyService;
  6. import ca.gaudraeult.mydropwizardapp.services.MyServiceImpl;
  7.  
  8. public class ServerModule extends AbstractModule {
  9.  
  10. @Override
  11. protected void configure() {
  12. bind(MyService.class).to(MyServiceImpl.class);
  13. }
  14. }

Dropwizard Application

If you remember from part 1 of this series you created the based Dropwizard app. So you should have a class called “MyDropwizardAppApplication”. Open that now and modify the “initialize” like the below. Baseically here we are registering our ServerModule class to Dropwizard.

  1. @Override
  2. public void initialize(final Bootstrap bootstrap) {
  3. bootstrap.addBundle(GuiceBundle.builder()
  4. .enableAutoConfig(this.getClass().getPackage().getName())
  5. .modules(new ServerModule())
  6. .build());
  7. }

And that is it you have configured a very basic Dropwizard Guice configuration.

Java: Basic Dropwizard Project

This entry is part 1 of 5 in the series Dropwizard

This tutorial will guide you through how to create a bare bones Dropwizard app. Ensure you have Eclipse installed or whatever IDE you are deciding to use. You can use their documentation as a guide. This is the first tutorial in the dropwizard series.

Setup Eclipse Archetype:

Select new Maven Project then in the select Archetype if dropwizard isn’t there you can add it by using the below settings.

Filter for Dropwizard Archetype:

Set Project Settings:

POM:

Ensure the pom has the correct “dropwizard.version” and that the dependency “dropwizard-core” is there.

Our Configuration:

Now that the project is created let’s have a look at what our configuration looks like. Pretty basic but that’s all we need right now.

  1. package ca.gaudreault.mydropwizardapp;
  2.  
  3. import io.dropwizard.Configuration;
  4. import com.fasterxml.jackson.annotation.JsonProperty;
  5. import org.hibernate.validator.constraints.*;
  6. import javax.validation.constraints.*;
  7.  
  8. public class MyDropwizardAppConfiguration extends Configuration {
  9. }

Our Application:

This is what our application class looks like. It’s empty nothing yet.

  1. package ca.gaudreault.mydropwizardapp;
  2.  
  3. import io.dropwizard.Application;
  4. import io.dropwizard.setup.Bootstrap;
  5. import io.dropwizard.setup.Environment;
  6.  
  7. public class MyDropwizardAppApplication extends Application {
  8.  
  9. public static void main(final String[] args) throws Exception {
  10. new MyDropwizardAppApplication().run(args);
  11. }
  12.  
  13. @Override
  14. public String getName() {
  15. return "MyDropwizardApp";
  16. }
  17.  
  18. @Override
  19. public void initialize(final Bootstrap bootstrap) {
  20.  
  21. }
  22.  
  23. @Override
  24. public void run(final MyDropwizardAppConfiguration configuration,
  25. final Environment environment) {
  26.  
  27. }
  28. }

Config.yml:

This is what our config.yml file looks like at the start.

  1. logging:
  2. level: INFO
  3. loggers:
  4. ca.gaudreault: DEBUG

Setup Debug Configuration:

Setup your debug configuration like the below setup.

Running:

Once you run it you will be running two sites.

  1. http://localhost:8080
    1. Your main site
  2. http://localhost:8081
    1. Your operational site (health, etc)