There's a neat little framework I've used recently called Spark. It is yet another
Java HTTP framework, but it's a very small and simple one.
I have written about Dropwizard in the past which is
also a small (compared to say, Spring) web framework, but Spark takes it to another level.
Here is a code snippet taken from their site:
public class HelloWorld {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello World");
}
}
That is all (ignoring Maven config) that's needed to start serving HTTP requests from the JVM.
I've not had the opportunity to use Spark in anything resembling a production environment so I cannot vouch for its reliability there.
However I have used it for small projects and it's been great doing zero setup before writing code that has an impact on the API's output.
Compare this to the equivalent endpoint using Express:
Uncanny, right? The Java sure as shit wouldn't have looked like that a few years ago...
I know of a few JavaScripters who would like to learn Java but find its learning curve off-putting. Spark may offer a foot in the door by helping to lower that learning curve. It provides a natural application structure far more familiar to those who've used Express in the past than any other framework I've seen so far.
They have easy to follow documentation on both setting upMaven and Spark itself, if you're interested.
As you may know, we have many REST APIs running on the JVM. The majority of which use the Spring MVC framework. We've very much a love/hate relationships with Spring (mostly hate).
An entire series of posts could be written about things we don't like about Spring, but to save time and sanity, I'll summarise as: too much boilerplate and configuration around merely getting the framework to serve HTTP requests. XML config files and ManagerServiceFactoryBeanImplFactory take time to understand and set up, leaving you less time to solve the problem that your application owes its existence to. We use annotations in most places to configure our Spring apps, but it's still a pain to have to deal with in general.
We've now moved two of our projects onto Dropwizard and are so far extremely happy with what we've seen.
What is Dropwizard?
It's a combination of things really as the Getting Started page will tell you. It provides Jetty for HTTP, Jackson for JSON, Jersey for REST and so on. The power and convenience of Dropwizard comes from it's hiding and pre-configuration of the above things right out of the box. To go from nothing to something serving API requests in Dropwizard doesn't require you to write a line of code setting up a Jetty or configuring Jackson.
Everything you need is exposed cleanly, everything you don't is hidden away. This leaves you to start writing code to solve problems almost immediately. The two projects I mentioned above were already using Jetty and Jersey, and so their migrations were fairly trivial. We will be moving Atlas Deer from Spring to Dropwizard sometime in the near future. I'll report back on how that goes and with any useful tips for moving from one to the other.
public class ExampleApplication extends Application<ExampleConfiguration> {
}
The above code, plus a very simple Configuration class and YAML, is all you need to start serving requests from an endpoint in Dropwizard. On top of that there is a long list of built-in, out of the box features that most if not all applications of this type would like.
An admin servlet is automatically configured and started, from which tasks and health checks can be easily built on top of. Other features include but are not limited to:
Logging
Object lifecycle management
Configuration validation
JVM and resource metrics
ASCII art printing at start up
All come for free and only require you to implement the bit you care about.
I'll save going into much more depth for after we've moved a Spring application onto Dropwizard, as I expect there will be far more to show. But so far, we would seriously recommend checking it out!