Java URL shortener service with Dropwizard and Redis

It never occurred to me I should write a Java URL shortener service with Dropwizard and Redis. Not because of Dropwizard or Redis, I just didn’t see why I should write a Java URL shortener at all.

That is until I read the blog post URL shortener service in 42 lines of code in… Java (?!) Spring Boot + Redis by Tomasz Nurkiewicz. Tomasz also references other interesting implementations of URL shortener services written in Scala, Clojure or Haskell.

Java URL shortener service with Dropwizard and Redis
Dropwizard logo

I knew from the start that an implementation based on Dropwizard would probably take a few more lines than one based on Spring Boot. Saving LoCs at all costs wasn’t the primary goal, though. I wanted to complete this exercise because I only recently wrote my first Dropwizard application – and because it was a very pleasant experience.

Somewhere I read a comment about Spring Boot which quite nicely expresses the thoughts I had when I first learned about its existence.

In short, this IS Dropwizard in every way. Spring 4 tries to become Dropwizard (and succeeds), JEE7 tries to become Spring 3 and kind of gets there. Everyone is becoming everyone else.

Eventhough Dropwizard could be tweaked to work with Spring (e.g. Spring Data Redis) this isn’t an option for me. If I want Spring, which I usually do, I’ll go with Spring Boot. Hence, this is pure JAX-RS with Dropwizard/Jersey and Jedis, a popular Java client for Redis.

The code

So, here’s the final solution on some 50 lines. Find a few comments below.

Line 17

The Jedis Wiki clearly states that “You shouldn’t use the same instance from different threads because you’ll have strange errors…To avoid these problems, you should use JedisPool, which is a threadsafe pool of network connections…You can store the pool somewhere statically, it is thread-safe.”

Line 22-23

This method is required by the Dropwizard Application class even though it does nothing. Also note that contrary to bigger Dropwizard applications which rely on configuration options there’s no need to implement your own Configuration sub class.

Line 33/47

For each Redis operation one should obtain a Jedis instance from the pool in a try-with-resources statement.

Line 44

Contrary to Tomasz solution I opted to POST the URL to shorten as a query parameter rather than a path parameter. I’m aware of a number of heated debates around the Internet how to do this “properly” with regards to REST principles and the HTTP spec.

Line 45

If you want to avoid the external dependency to UrlValidator you could do something like

instead. However, be aware that you’d then have to handle the checked MalformedURLException declared by the URL constructor.

The Maven build

The pom.xml declares only 4 dependencies

If you use the Maven Shade Plugin as recommended by Dropwizard you can build a single fat JAR which contains all dependencies listed above.


Building a Java URL shortener service with Dropwizard and Redis is not difficult and you won’t have to sacrifice readability. For me this has always been way more important than saving characters or lines while coding.

Leave a Reply