它是一个开源的微型JavaWeb框架,拥有最少的依赖,以及快速的学习曲线。该项目的目标是创建一个微型的Web框架在Java中这应该是易于使用和破解。
其概念是不是新的(灵感来自于Sinatra,ExpressJS,PlayFramework),但目的是提供一个清晰,简单易用的模块化解决方案。pippo可用于中小型应用,并应用在基于微服务架构的应用程序中。
示例代码:
publicclassSimpleDemo { publicstaticvoidmain(String[] args) {// new Pippo().start(); // run the default web server with the default web server settings Pippo pippo =newPippo(newSimpleApplication()); pippo.getServer().getSettings().staticFilesLocation("/public"); pippo.start(); } } publicclassSimpleApplicationextendsApplication { @Override publicvoidinit() { super.init(); GET("/",newRouteHandler() { @Override publicvoidhandle(Request request, Response response) { response.send("Hello World"); } }); GET("/file",newRouteHandler() { @Override publicvoidhandle(Request request, Response response) { response.file(newFile("pom.xml")); } }); GET("/json",newRouteHandler() { @Override publicvoidhandle(Request request, Response response) { Contact contact =newContact() .setName("John") .setPhone("0733434435") .setAddress("Sunflower Street, No. 6"); // you can use variant 1 or 2// response.contentType(HttpConstants.ContentType.APPLICATION_JSON); // 1// response.send(new Gson().toJson(contact)); // 1 response.json(contact);// 2 } }); GET("/template",newRouteHandler() { @Override publicvoidhandle(Request request, Response response) { Map<String, Object> model =newHashMap<String, Object>(); model.put("greeting","Hello my friend"); response.render("hello.ftl", model); } }); GET("/error",newRouteHandler() { @Override publicvoidhandle(Request request, Response response) { thrownewRuntimeException("Errorrrrrrrr..."); } }); } }
评论