Pippo - Micro Java Web Framework
Join the DZone community and get the full member experience.
Join For FreeAfter 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.
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(); } }
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"); } }
public class Contact { private int id; private String name; private String phone; private String address; // getters and setters }
public class BasicDemo { public static void main(String[] args) { Pippo pippo = new Pippo(new BasicApplication()); pippo.start(); } }
- http://localhost:8338
- http://localhost:8338/file
- http://localhost:8338/json
- http://localhost:8338/xml
- http://localhost:8338/negotiate
- http://localhost:8338/template
Useful links
https://github.com/decebals/pippo
http://www.pippo.ro
Opinions expressed by DZone contributors are their own.
Trending
-
Microservices With Apache Camel and Quarkus
-
Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
-
Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
-
Automating the Migration From JS to TS for the ZK Framework
Comments