Over a million developers have joined DZone.

Modern Java Web Dev: Using Dropwizard From the Start – Part 1

What are Dropwizard, Spring Boot, and other modern Java web development frameworks useful for? This series explores that question, starting with Dropwizard.

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

In 2014 I wrote a scathing article about how much I hate Spring. You can read it here, but the tl;dr is this: I hate programming in XML, I don’t like magic, and I don’t like frameworks. Whenever I’ve used Spring or similar frameworks I’ve been burnt and ended up worse for it.

But that was all the way back in early 2014. Since then I’ve handled my own Dependency Injection, using TDD and having a top level class that handles building the injection. Then, over Thanksgiving weekend, the internet discovered my post and it went viral. I had over 10k hits in the first 2 days. Twitter and Reddit both got supportive and angry and the flamewars began. It was great fun and very interesting for me. It turns out a lot has changed in the last 2 years.

The most interesting thing for me came out of a discussion with Phil Webb from the Spring team, where he posited that Spring isn’t about DI anymore; it’s about having everything you need to build a fully fledged web app, and DI is just a side note. It turns out Spring isn’t the only framework in this space; the main ones I’ve picked out of the discussion are:

- Spring/Spring Boot

- Dropwizard

- Wicket

If you have something else to throw into the mix then please let me know in the comments below and I’ll add it to the list.

At this point I’m really confused why these solutions have been built. They seem like an answer to a question that hasn’t been asked. From experience I can put together a working, scalable enterprise app with a lot less cruft and a much easier learning curve in very little time. But, I feel there must be something I’m missing. As a result I’m starting an experiment, to go from zero knowledge to fully fledged application using these frameworks. I hope to learn something new, and hopefully spot what I’m missing.

How’s it Going to Work?

I’m going to build a chat application. It’s a really well understood domain, and in its purest form it should be easy enough to put together over the course of a few articles. I’m going to start with Dropwizard, as I’ve zero experience with it and so shouldn’t be bias, before moving onto Spring, then who knows what. It won’t be a full on enterprise application, but it will hopefully be usable and realistic enough to present a legitimate example.

“You’re doing it wrong”

This is inevitably going to happen. I’ll be working from the Hello World documentation and working my way up. I’m not necessarily going to hit best practices as I’m going to be trying to learn the frameworks in a short amount of time. Use the comments section and be gentle. Feel free to tell me what I’ve missed or what I should add on. I can add an addendum to the article.

How can I follow along?

I post all my DZone articles to my twitter account @SambaHK. Follow me on there to ensure you see all the updates, or just check DZone all the time (which you do already right?)

Dropwizard Part 1: Hello World

What on earth is DropWizard? From the docs:

“Dropwizard is a Java framework for developing ops-friendly, high-performance, RESTful web services.”

It pulls together established libraries into a single framework which all works together nicely and saves you needing to worry about it. This covers everything from HTTP & REST to performance measurement and UI templating. Clearly it’s an opinionated framework up front. I can see this would be useful to someone looking to get going quickly, but I’m interested to see how easy it is to swap these parts out if I want to try something different.

There is follow along hello world documentation available here. On first look I won’t lie; I’m concerned. To get the basic hello world working requires about 4 different classes and a whole bunch of boilerplate. It’s important not to judge a framework on zero to hello world speed, but it still feels a bit grim.

The example used on the site is actually a bit more complicated than it needs to be as it’s introducing multiple concepts at once. Having (eventually) gotten it working, I’m going to show you the simple version first. To get to this point I had to go through the fun of mysterious framework errors;  So far it seems like the documentation for Dropwizard isn't great, but I suspect if I googled for the composite parts (eg "Jetty XXXException" instead of "Dropwizard XXXException) I might have more luck.

A resource

Dropwizard uses Jersey, a framework for developing RESTful webservices in Java. We create a Resource which we register with Jersey which represents a Rest end point.

@Path("/hello-world")
@Produces(MediaType.TEXT_PLAIN)
public class HelloWorldResource {

  @GET
  public String sayHello(@QueryParam("name") Optional<String> name) {
      return "Hello World";
  }
}





public class HelloWorldApplication extends Application<HelloWorldConfiguration> {

  public static void main(String[] args) throws Exception {
  new HelloWorldApplication().run(args);
  }

  @Override
  public String getName() {
  return "hello-world";
  }



  @Override
  public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
  // nothing to do yet
  }



  @Override
  public void run(HelloWorldConfiguration configuration, Environment environment) {
    final HelloWorldResource resource = new HelloWorldResource();
    environment.jersey().register(resource);
  }



}


Running this will get you a working RESTful endpoint. It also throws in an admin console with Threads, Metrics and Healthcheck (more on that in a later post) for free, which is pretty nice.

One of the things I’m already not a fan of here though is that the Main class has to extend the Application class, part of dropwizard core. It seems this inheritance occurs a lot in Dropwizard, which is a code smell for me. This is not a hands off framework and if you ever want to backout I think it would be hard to unwind from. It also requires the existence of a Configuration class. This can be used to include config from a YAML file, but I’ve chosen to ignore this and have an empty config class for now to keep it simple.  I'm hoping that I can actually rejig the app so that it uses composition instead of inheritance, but for now I wanted to keep it simple by following the documentation.

Jackson, the JSON parser is included as part of the Dropwizard family. We can change the response to, instead of returning text, returning a JSON object. If our method returns a POJO, which we annotate appropriately, the conversion will happen automatically.

public class Saying {
  private long id;

  @Length(max = 3)
  private String content;

  public Saying() {
      // Jackson deserialization
  }

  public Saying(long id, String content) {
    this.id = id;
    this.content = content;
  }

  @JsonProperty
  public long getId() {
      return id;
  }

  @JsonProperty
  public String getContent() {
      return content;
  }

}



@GET
public Saying sayHello(@QueryParam("name") Optional<String> name) {
return new Saying(counter.incrementAndGet(), “Hello World!”);
}

So now we have Hello World returning in a proper JSON format.

It’s only Hello World and we’ve got a long way to go. It’s clear that Dropwizard has the potential to be powerful and has a lot of stuff thrown in so that the developer doesn’t need to worry about it. Keep checking DZone or follow me via @SambaHK on twitter for the next edition as I try to take hello world to a chat app and explore what else Dropwizard has to offer.





Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:
java ,dropwizard ,web framework

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}