| layout | default |
|---|---|
| title | Pippo |
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.
Pippo can be used in small and medium applications and also in applications based on micro services architecture.
We believe in simplicity and we will try to develop this framework with these words in mind.
The core is small (around 140 KB) and we intend to keep it as small/simple as possible and to push new functionalities in pippo modules and third-party repositories/modules.
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).
Also, Pippo comes with a very small footprint that makes it excellent for embedded devices (Raspberry Pi for example).
The framework is based on Java Servlet 3.1 and requires Java 8.
Talk is cheap. Show me the code.
Add some routes in your application:
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"); // template's model/contextrouteContext.render("hello");
});
}
privateContactcreateContact() {
returnnewContact()
.setId(12345)
.setName("John")
.setPhone("0733434435")
.setAddress("Sunflower Street, No. 6");
}
}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);
}
}publicclassBasicDemo {
publicstaticvoidmain(String[] args) {
Pippopippo = newPippo(newBasicApplication());
pippo.start();
}
}See Getting started section for some basic information.