Over a million developers have joined DZone.

Pippo - Micro Java Web Framework

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

A few month ago I started to work on a personal small web project in Java. My intention was to use my application on a micro computer (Raspberry PI). There are many (maybe too many) powerful java web frameworks, yet I couldn't find a java micro web framework to satisfy me (with a small footprint, easy to learn and use).

After a period of research and study of other micro frameworks (java and non java - javascript, python) I created Pippo. It's instructive to refresh your knowledge with HTTP/HTTPS concepts and to be cutting edge (Servlet 3.x) :) Also this framework has an educational role for my younger colleagues.

Trying to create an open source micro java web framework which is also simple to learn as it is to use was the first thing taken into account when Pippo began its journey.

From the beginning I wanted this to be understandable and hackable. These means that you must read very few (some examples code) to produce something useful and you can add your contribution to framework in short time. You are a busy developer, you want to accomplish a task not to read books.

I am for transparency. I don’t want to hide the Request-Response nature of the HTTP protocol and to add unnecessary layers that complicate things. We play with solid concepts like: Application, Request, Response, Route, Router, RouteHandler, RouteContext, RouteDispatcher.

Pippo can be used in small and medium applications and also in applications based on micro services architecture.

I believe in simplicity and I tried to develop this framework with these words on my mind.

The core is small (around 100k, with a single tiny dependency slf4j-api) and we intend to keep it as small/simple as possible and to push new functionalities in pippo modules and third-party repositories/modules. Still, it comes with many features and allows the addition of new features.

The framework comes with many useful modules (Spring, Guice, Metrics, Session cookie, Controller, Content type engines - Json,Xml, Yaml, Text) and many demo applications.

You are not forced to use a specific template engine or an embedded web server. Furthermore you have multiple out of the box options (see Templates and Server sections from the documentation site).

I think that Pippo does a great job in mixing server generated pages with RESTful APIs. We have a demo that shows you Pippo AngularJS integration demo.

Enough with the introduction. It’s time for “show me the code”.

See below the classic “Hello World” in Pippo using the embedded web server:

public class HelloWorld {

    public static void main(String[] args) {
        Pippo pippo = new Pippo();
        pippo.getApplication().GET("/", (routeContext) -> routeContext.send("Hello World!"));
        pippo.start();
    }

}
You can run HelloWorld from your IDE (or command line) as a normal (desktop) application.
The default port for the embedded web server is 8338 so open your internet browser and type http://localhost:8338 to see the result.

I will show you a more complex example. We split our application in two parts for a better readability.

First we must create a BasicApplication (extends Application) and add some routes:

public class BasicApplication extends Application {

    @Override
    protected void onInit() {
        // send 'Hello World' as response
        GET("/", (routeContext) -> routeContext.send("Hello World"));

        // send a file as response
        GET("/file", (routeContext) -> routeContext.send(new File("pom.xml"));

        // send a json as response
        GET("/json", (routeContext) -> {
            Contact contact = createContact();
            routeContext.json().send(contact);
        });

        // send xml as response
        GET("/xml", (routeContext) -> {
            Contact contact = createContact();
            routeContext.xml().send(contact);
        });

        // send an object and negotiate the Response content-type, default to XML
        GET("/negotiate", (routeContext) -> {
            routeContext.xml().negotiateContentType().send(contact);
        });

        // send a template as response
        GET("/template", (routeContext) -> {
            routeContext.setLocal("greeting", "Hello");
            routeContext.render("hello");        
        });
    }

    private Contact createContact() {
        return new Contact()
            .setId(12345)
            .setName("John")
            .setPhone("0733434435")
            .setAddress("Sunflower Street, No. 6"); 
    }

}
where Contact is a simple POJO:
public class Contact  {

    private int id;
    private String name;
    private String phone;
    private String address;

    // getters and setters

}
The last step it's to start Pippo with your application as parameter:
public class BasicDemo {

    public static void main(String[] args) {
        Pippo pippo = new Pippo(new BasicApplication());
        pippo.start();
    }

}
Pippo launches the embedded web server (found in your classpath) and makes the application available on port 8338 (default value). Open your internet browser and check the routes declared in Application:
There is so much more to say about Pippo, but this article is only meant as a first contact. If you feel I have aroused your curiosity and you’d like to find out more about it, then these few lines have achieved their goal.

Useful links
https://github.com/decebals/pippo
http://www.pippo.ro 

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

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 }}