Dropwizard is hot and ready to serve resources so it’s time to create the first HTTP API endpoint and make our app useful. In this post I will prepare endpoint which return random clean code rule.

Resource

Dropwizard have many features but the core of this library/framework are resources. Each resource is related with URI and represent some business logic. Our first resource will return random clean code rule instead of classic Hello World.

Annotation @Path(“/rules”) is crucial to provide API endpoint. It tells Jersey (do not mislead with Jersey Shore Massacre) that /rules URI is registered and is associated with methods of this Java class resource.

By adding@Produces(MediaType.APPLICATION_JSON) annotation return data will have JSON format.

Resource constructor has one parameter – clean code rules repository which is a rules storage accessor (will back to this later).

The final and the most important is a presentRandomRule method with @GET annotation. This method will return one random clean code rule or default rule (if no rules defined) when user will call /rules resource via GET HTTP request.

@Timed is an extra (not required) annotation related with Dropwizard metrics module. With this annotation resource method will be added to the metrics statistics in admin panel (I mentioned about Dropwizard admin panel in Run Dropwizard with Gradle).

Clean Code rules

To present data we need an object which represents clean code rule. To do not complicate the example the Rule class contains two fields: title and description.

Just three remarks here:

  • this class is immutable (important in multithreading environment),
  • each getter should be annotated with @JsonProperty (from Jackson) to provide (de)serialisation from/to JSON,
  • empty constructor is necessary.

Rules will be provided by repositoryRulesRepository. At this moment this repository will have just one method (random) to return random rule from all the defined rules.

To make it easy we will read rules from JSON file stored in source resources directory.

And the repository implementation:

Make it alive!

To start using the new resource we should register it in jersey on application start. Firstly we must to initialise rules repository:

final JsonFileRulesRepository rulesRepository = new JsonFileRulesRepository();

Next initialise rules resource:

final RulesResource rulesResource = new RulesResource(rulesRepository);

and as a final step register resource in environment:

environment.jersey().register(rulesResource);

The full Dropwizard application class:

After this few lines resource should be available to use. Let’s start application via Gradle run task and open http://localhost:8080/rules endpoint.

Output should be similar to this one:

{"title":"Dead code","description":"Do not keep unused or commented code. At any time you can back to this code in repository."}

Additionally as I mentioned before the new resource method should be added to the metrics in the timers sections:

"timers" : {
    "com.karollotkowski.cleancode.resources.RulesResource.presentRandomRule" : {
      "count" : 2,
      "max" : 0.0046128720000000005,
      "mean" : 0.0023089918465641706,
      "min" : 3.9412E-5,
      "p50" : 3.9412E-5,
      "p75" : 0.0046128720000000005,
      "p95" : 0.0046128720000000005,
      "p98" : 0.0046128720000000005,
      "p99" : 0.0046128720000000005,
      "p999" : 0.0046128720000000005,
      "stddev" : 0.0022866656872260814,
      "m15_rate" : 0.07430150548563609,
      "m1_rate" : 4.326376622914337E-12,
      "m5_rate" : 0.0025637333785025547,
      "mean_rate" : 0.001311739347512153,
      "duration_units" : "seconds",
      "rate_units" : "calls/second"
    },

Code

All the code related with this example you can find on my GitHub profile in this commit.

Happy coding!

Advertisements