Simple web services and Java

Monday night a few weeks ago found me mashing my keyboard in frustration. I had spent most of the day trying to figure out how to build a dead simple web service in Java. I knew how to do it in Node.js, in Python, in Ruby, in Go, and in Clojure. I even knew how to write a complex API using gRPC and Protocol buffers in Java. A simple web API, however, was proving to be a different story altogether.

I wanted something similar to the libraries in the aforementioned languages – the ability to define an API of simple routes and their corresponding handlers, no extraneous framework-y fluff.

The following recounts how it went.


An initial Google search leads me to Jersey. I vaguely remember it from my last job as something the backend devs kept saying they were in the middle of migrating; nevertheless, I click through to the home page, and open up a few tutorials alongside for good measure.

The Getting Started guide mentions a whole bunch of things: “maven archetypes” (I’m using Gradle, so I’m not sure what that maps to), a “Grizzly container”… okay, looks like I need to write some classes and annotate them. A little verbose, but it could work. Let me check on StackOverflow on how to set this up with Gradle. Ah, so that’s the dependency I need! Now how do I fire up the server? Oh, I need a config file. Wait, XML to configure the server?! Nope. Nuh-uh.

The next thing that catches my eye is Spring Boot, having heard it mentioned by nearly every Java developer I know. Their home page tells me that it is “designed to get you up and running as quickly as possible”. Sounds good. Their “quick start” clearly mentions the dependency need for Gradle. The example looks too much like the one from Jersey though, and the bad taste left in my mouth by that still lingers. I’ll pass for now.

DropWizard is another name that comes by; let’s see what that feels like. The home page describes itself as a “stable, light-weight package that lets you focus on getting things done.” I do, indeed, have things that need getting done, so let’s take a look!

The guide declaims their love for all things Maven. It’s not too hard to convert a single Maven-style dependency into a Gradle one, so I plough on. I need a “configuration class” it appears. Once again, annotations abound. Then I also need an “application class”. And a “representation class”. And a “resource class”, which is the “meat and potatoes” of the application, you say?

Arghhh.

I’m this close to giving up. Is it really that difficult to elegantly expose one single endpoint in Java without wanting to kill myself?

Almost at the end of my patience, I decide to Google just that. Lo and behold, there’s a Quora question with that exact sentiment (probably not that surprising in retrospect). Normally I avoid Quora as much as possible because of their dark UX habits but the title sums up my state of mind with such precision that I can’t resist.

All the usual suspects are present. I skim past them. Somewhere near the bottom is a link without a description – Spark Framework. Hoping that it has nothing to do with the data processing engine (since I don’t think that can solve the problem at hand), I click through.

There is a nice big example right on top:

import static spark.Spark.*;

public class HelloWorld {
    public static void main(String[] args) {
        get("/hello", (req, res) -> "Hello World");
    }
}

It’s five lines, has a simple handler without annotations, and even uses Java 8 lambda-style functions! I think I’m going to cry tears of joy!

Less than five minutes later, I have my simple web service up and running.


And that sums up my adventures with writing a web service in Java.

A full year

2016 started in Kyoto, Japan. I rarely celebrate New Year at all so a cozy home-made Japanese dinner with our AirBnB host who tried to get us to sample all the alcohol she owned from various parts of the country despite our half-hearted protests (“we have a train to catch early in the morning!”) was a perfect way to celebrate. The entire trip was great; a full post about it is in order.

In March, I left Autodesk where I’d been working since my graduation. The environment was relaxed and I miss some of the people there (and all the table tennis I haven’t played since I left) but I’m glad I made the change. I’m at ThoughtWorks now where I get to work on more interesting projects with some pretty smart people, and I feel like I’ve already learned more than I did at Autodesk.

Talking about work, I also tried my hand at teaching – from August to October, I taught a part-time web development course at General Assembly. I’ve never formally taught before and I enjoyed figuring out how to explain all these things I’ve gotten so used to over fifteen years into ten lessons for people who are trying them out for the first time. It was super tiring, made me appreciate my schoolteachers more, and was lots of fun!

Also for the first time, I travelled to Bangalore for work twice thrice. Thought it doesn’t feature on my Terribly Exciting Places to Visit list, the combination of good weather, bad traffic, we-close-at-eleven nightlife, and meetings with a friend made it rather enjoyable. One of the trips collided with the aforementioned teaching job, leading to an exhausting interesting time when I came back to Singapore on the morning of a class, flew to Bangalore the next day, and then returned on the following weekend in time for the next class. Whee–ouch.

The recreational travel front wasn’t as bright as the year before though. We went to Cameron Highlands in August after postponing the trip twice (a hill station at last!). The place, unlike the bus ride to it, was comfortable and convenient. In September, we did a short trip to Batam, which I can thoroughly recommend for a weekend getaway. I also went back home for Diwali because I’d forgotten about the life-threatening smoke, but we were at my grandparents’ in Shangarh for most of the time so it was all good – great, in fact.

Mid-year also witnessed a change in my living scenery. G and I elected to find a place together and our hunt for a place “somewhere central” brought us to Chinatown (thanks, 99.co!). Living in the middle of the city is fantastic – everything is a stone’s throw away. I can walk to work! Record stores and nice cafés are just round the corner! FoodPanda actually told me that they had “too many restaurants nearby” and suggested I filter them! Braddell with its tall trees and adequately-spaced buildings is being missed, but did I mention how nearby everything is now and that I walk to work?!

That brings us to the tail end of the year. It was shaping up to be quite uneventful (I’m in Singapore, which has has happened only once before despite my living here for six years) so I compensated by discarding many many many plastic pieces of paper from my wallet in lieu of a new record player, the beginnings of a record collection, a new set of speakers (I’ll miss you, beautiful Swans!) – the works, essentially. Can’t say I regret it at all, to be honest!

Here’s wishing you a happy new year too!

Looking for more posts? Check out the archives.