Micra: A simple Java Web framework

Java powers a lot of webapps, but I’ve always found it to be fairly unwieldy to get a project started, especially in comparison to the simplicity of Ruby, PHP, or server-side JavaScript (Node.js). Even a simple Hello World in Spring MVC takes a lot of work. This contrasts with the three steps it takes to create and run a Sinatra web site in Ruby.

Inspired by Java 8’s lambdas, I created Sinatra-like Java Web framework as a proof-of-concept that takes advantage of the new anonymous functions. This is how you write your first Web page:

get("/", (req, resp) -> "Java, sans fluff.");

This is part of a larger class (since everything’s a class in Java):

public class HelloWorldServlet extends MicraServlet
{
    public HelloWorldServlet()
    {
        get("/", (req, resp) -> "Java, sans fluff.");
    }
}

You can view the full Micra source over on GitHub. You should know that this is not production worthy nor is it my area of expertise. I welcome criticism and advice. I would also love someone adopting the idea of a simple, lambda-based Java Web framework and running with it. The idea that you could build out a simple webapp in five minutes in Java is frankly thrilling.

I should note I like the Spark framework in Java, but I also like lambdas, so that’s why I felt the need to try them out with this framework. With some work, I assume Spark could be adapted to use lambdas.

You can deploy this app to Heroku right now, as is. It can create a fat jar with Jetty to run it (which is how you deploy to Heroku). You can also build a war and deploy to a servlet container.

Why Java

Personally, I’m attracted to Java as a language. There’s a huge number of developers that know Java and consider it their primary language, and they contribute on Stack Overflow, with their blogs, and by open-sourcing their code. I know people that love, love Java (I don’t know too many people who love PHP). Almost every database has a Java driver and almost every service has a Java client. You can develop on Windows/Linux/Mac with same IDE (IntelliJ, Eclipse, and NetBeans).

Beyond that, Java is fast, and it’s really nice to develop on a fast platform. If you use the correct setup, your application can run circles around other languages and platforms. There’s a reason that when load scaled out to millions of users, Google, LinkedIn, and Twitter turned to the JVM. Unlike Microsoft .NET, the JVM is available on Linux (Mono is available on Linux but it’s not as fast as Microsoft’s implementation), so you can deploy to pretty much any server and it’s really cheap.

Why Java is also a stumbling block

Every time I try to work in Java, I’m pulled back by the siren song of C#. The little things get to me quickly. I wish Java had properties, extension methods, and LINQ. Those save me so much time and make programming a pleasure. I truly feel addicted. Whenever I want to program in Java, I think, “Well, I could spend forever typing this thing out, or I could use C# and go onto something more interesting.”

I miss some critical C# libraries when using Java, specifically Dapper (a micro-ORM). I love Dapper. Why? It’s simple to use and it’s faster than straight ADO.NET in some cases. If you want to use Entity Framework (the flagship .NET ORM) for the majority of your work but need speed for a feature, you can drop down to Dapper and use SQL. Speaking of Entity Framework, that’s another amazing tool. When I first used EF during version 1 and 2, I thought it was a terrible idea (it was – you had to use a visual designer). Now that I can simple write a POCO that will be made into a table, I think it’s great. It’s also dead simple to write a unit test for your application when using EF (yes, I mean unit test. You don’t have to have a SQL Server instance running to test your queries – you can mock your database with basically Lists).

I find Visual Studio to be the easiest, most powerful IDE out there. Perhaps I need to spend a week just learning IntelliJ, but I don’t have the time or motivation for that when I already have my needs met.

If I don’t want to use a full-fledged statically-typed, compiled language, I build what I need to in Python using Flask. I can whip out a webapp in hours and deploy it to nginx without (too much) pain. Python is another language with a huge community, tons of libraries, and it’s fairly sensical. When I was first learning Flask, it took about twenty minutes to get started, and the learning path is fairly linear (meaning it doesn’t jump in complexity to learn more advanced topics). Sometimes the libraries and/or their documentation suck, but I’ve had so much less pain from Python than Java.

This all to say, if I’m planning on building a bigger project where I’d like type checking and compilation, I use C# / MVC. If I want something simple with no fuss, I use Python / Flask. That leaves Java in cold. The JVM may be slightly faster on Linux than Mono, but when it comes to an internal app with a handful of users, it’s good enough.

If I had 100K in funding, and I was building an app that needed to scale to millions on the cheap, and knew I could spend 6 months before I needed to show something useful, I might use Java. On the other hand, if I need to build something that’s useful right now, I would use C#. My hope is that with this proof-of-concept and other efforts, we can get to a point where Java’s competitive with C# at least in Web frameworks.

Tagged with:
Posted in IT
4 comments on “Micra: A simple Java Web framework
  1. Phil Webb says:

    You might want to look at Spring Boot (http://projects.spring.io/spring-boot/) which attempts to address some of the same concerns. The equivalent application in boot would be:

    @RestController
    @EnableAutoConfiguration
    public class HelloWorld {

    @RequestMapping(“/”)
    public String home() {
    return “Java, sans fluff”;
    }

    public static void main(String… args) {
    SpringApplication.run(HelloWorld.class);
    }
    }

    It’s even less code with Groovy: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-cli-example

    (disclaimer: I work on Spring Boot)

  2. Noel says:

    Wow, thanks! I’ll try that out. The examples look very clean and simple.

  3. David says:

    I guess you’re probably aware, but Spark supports Java 8 now 🙂

  4. Lairton Ballin says:

    Now, about two years after your initial post, there is a great chance that you have come across sql2o (http://www.sql2o.org/), a Dapper-like library for Java, on your own.. I thought worth mentioning it anyway. Now, regarding the usage of Visual Studio as you favorite IDE, you might want to check Integra Studio (http://www.softerg.com/integra/). Disclaimer: I’m not affiliated with none of them. I’m just a fan that used both.

Leave a Reply

Your email address will not be published. Required fields are marked *

*