Skip to content

Repository files navigation

Micro Java Web Framework

Join the chat at https://gitter.im/decebals/pippoGitHub Actions StatusCoverage StatusMaven Central

It's an open source (Apache License) micro web framework in Java, with minimal dependencies and a quick learning curve.
The goal of this project is to create a micro web framework in Java that should be easy to use and hack.
The size of pippo-core is only 140 KB and the size of pippo-controller (optional) is only 45 KB.

Sample code

1. Routes approach

First we must create an Application and add some routes:

publicclassBasicApplicationextendsApplication {
@OverrideprotectedvoidonInit() {
// send 'Hello World' as responseGET("/", routeContext -> routeContext.send("Hello World"));
// send a file as responseGET("/file", routeContext -> routeContext.send(newFile("pom.xml")));
// send a json as responseGET("/json", routeContext -> {
Contactcontact = createContact();
routeContext.json().send(contact);
});
// send xml as responseGET("/xml", routeContext -> {
Contactcontact = createContact();
routeContext.xml().send(contact);
});
// send an object and negotiate the Response content-type, default to XMLGET("/negotiate", routeContext -> {
Contactcontact = createContact();
routeContext.xml().negotiateContentType().send(contact);
});
// send a template with name "hello" as responseGET("/template", routeContext -> {
routeContext.setLocal("greeting", "Hello");
routeContext.render("hello");
});
}
privateContactcreateContact() {
returnnewContact()
.setId(12345)
.setName("John")
.setPhone("0733434435")
.setAddress("Sunflower Street, No. 6");
}
}

where Contact is a simple POJO:

publicclassContact {
privateintid;
privateStringname;
privateStringphone;
privateStringaddress;
// getters and setters
}

The second step is to choose your favorite server, template engine and content type engine.
For example, I will choose Jetty as server, Freemarker as template engine, Jackson as JSON engine and JAXB as XML engine.
My Maven pom.xml looks like:

<dependency>
<groupId>ro.pippo</groupId>
<artifactId>pippo-core</artifactId>
<version>${pippo.version}</version>
</dependency>
<dependency>
<groupId>ro.pippo</groupId>
<artifactId>pippo-jetty</artifactId>
<version>${pippo.version}</version>
</dependency>
<dependency>
<groupId>ro.pippo</groupId>
<artifactId>pippo-freemarker</artifactId>
<version>${pippo.version}</version>
</dependency>
<dependency>
<groupId>ro.pippo</groupId>
<artifactId>pippo-jackson</artifactId>
<version>${pippo.version}</version>
</dependency>

The last step it's to start Pippo with your application as parameter:

publicclassBasicDemo {
publicstaticvoidmain(String[] args) {
Pippopippo = newPippo(newBasicApplication());
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:

  • http://localhost:8338
  • http://localhost:8338/file
  • http://localhost:8338/json
  • http://localhost:8338/xml
  • http://localhost:8338/negotiate
  • http://localhost:8338/template

2. Controllers approach

Define controller(s):

@Path("/contacts")
@LoggingpublicclassContactsControllerextendsController {
privateContactServicecontactService;
publicContactsController() {
contactService = newInMemoryContactService();
}
@GET@Named("index")
// @Produces(Produces.HTML)@Metered@Loggingpublicvoidindex() {
// inject "user" attribute in sessiongetRouteContext().setSession("user", "decebal");
// send a template with name "contacts" as responsegetResponse()
.bind("contacts", contactService.getContacts())
.render("contacts");
}
@GET("/uriFor/{id: [0-9]+}")
@Named("uriFor")
@Produces(Produces.TEXT)
@TimedpublicStringuriFor(@Paramintid, @HeaderStringhost, @SessionStringuser) {
System.out.println("id = " + id);
System.out.println("host = " + host);
System.out.println("user = " + user);
Map<String, Object> parameters = newHashMap<>();
parameters.put("id", id);
Stringuri = getApplication().getRouter().uriFor("api.get", parameters);
return"id = " + id + "; uri = " + uri;
}
@GET("/api")
@Named("api.getAll")
@Produces(Produces.JSON)
@NoCachepublicList<Contact> getAll() {
returncontactService.getContacts();
}
@GET("/api/{id: [0-9]+}")
@Named("api.get")
@Produces(Produces.JSON)
publicContactget(@Paramintid) {
returncontactService.getContact(id);
}
}
@Path("/files")
publicclassFilesControllerextendsController {
@GETpublicvoidindex() {
// send a template with name "files" as responsegetRouteContext().render("files");
}
@GET("/download")
publicFiledownload() {
// send a file as responsereturnnewFile("pom.xml");
}
@POST("/upload")
@Produces(Produces.TEXT)
publicStringupload(FileItemfile) {
// send a text (the info about uploaded file) as response// return file.toString();returnnewStringBuilder()
.append(file.getName()).append("\n")
.append(file.getSubmittedFileName()).append("\n")
.append(file.getSize()).append("\n")
.append(file.getContentType())
.toString();
}
}

Add controller(s) in your application:

publicclassBasicApplicationextendsControllerApplication {
@OverrideprotectedvoidonInit() {
addControllers(ContactsController.class); // one instance for EACH request// ORaddControllers(newContactsController()); // one instance for ALL requestsaddControllers(FilesController.class);
}
}

Don't forget that the Controller concept is included in pippo-controller module so you must add this module as dependency in your project.

Documentation

Documentation is available on pippo-java.github.io

Demo

Demo applications are available on pippo-demo
For a real life application built with Pippo please look at Web Accounting - Pippo Demo

About

Micro Java Web Framework

Topics

Resources

Stars

787 stars

Watchers

40 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages