pippo
1.0.0
這是Java中的開源(Apache許可證)Micro Web框架,具有最小的依賴性和快速學習曲線。
該項目的目的是在Java中創建一個微型Web框架,該框架應該易於使用和入侵。
pippo-core的大小僅為140 kb ,而pippo-controller (可選)的大小僅為45 kb 。
首先,我們必須創建一個應用程序並添加一些路線:
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 -> {
Contact contact = createContact ();
routeContext . xml (). negotiateContentType (). send ( contact );
});
// send a template with name "hello" 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" );
}
} Contact是一個簡單的pojo:
public class Contact {
private int id ;
private String name ;
private String phone ;
private String address ;
// getters and setters
}第二步是選擇您喜歡的服務器,模板引擎和內容類型引擎。
例如,我將選擇Jetty作為服務器, Freemarker作為模板引擎, Jackson為JSON Engine, JAXB作為XML引擎。
我的小pom.xml看起來像:
< 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 >最後一步是以您的應用程序作為參數啟動Pippo :
public class BasicDemo {
public static void main ( String [] args ) {
Pippo pippo = new Pippo ( new BasicApplication ());
pippo . start ();
}
} Pippo啟動了嵌入式Web服務器(在您的類路徑中找到),並在端口8338 (默認值)上提供該應用程序。打開互聯網瀏覽器並檢查應用程序中聲明的路線:
http://localhost:8338http://localhost:8338/filehttp://localhost:8338/jsonhttp://localhost:8338/xmlhttp://localhost:8338/negotiatehttp://localhost:8338/template定義控制器:
@ Path ( "/contacts" )
@ Logging
public class ContactsController extends Controller {
private ContactService contactService ;
public ContactsController () {
contactService = new InMemoryContactService ();
}
@ GET
@ Named ( "index" )
// @Produces(Produces.HTML)
@ Metered
@ Logging
public void index () {
// inject "user" attribute in session
getRouteContext (). setSession ( "user" , "decebal" );
// send a template with name "contacts" as response
getResponse ()
. bind ( "contacts" , contactService . getContacts ())
. render ( "contacts" );
}
@ GET ( "/uriFor/{id: [0-9]+}" )
@ Named ( "uriFor" )
@ Produces ( Produces . TEXT )
@ Timed
public String uriFor ( @ Param int id , @ Header String host , @ Session String user ) {
System . out . println ( "id = " + id );
System . out . println ( "host = " + host );
System . out . println ( "user = " + user );
Map < String , Object > parameters = new HashMap <>();
parameters . put ( "id" , id );
String uri = getApplication (). getRouter (). uriFor ( "api.get" , parameters );
return "id = " + id + "; uri = " + uri ;
}
@ GET ( "/api" )
@ Named ( "api.getAll" )
@ Produces ( Produces . JSON )
@ NoCache
public List < Contact > getAll () {
return contactService . getContacts ();
}
@ GET ( "/api/{id: [0-9]+}" )
@ Named ( "api.get" )
@ Produces ( Produces . JSON )
public Contact get ( @ Param int id ) {
return contactService . getContact ( id );
}
} @ Path ( "/files" )
public class FilesController extends Controller {
@ GET
public void index () {
// send a template with name "files" as response
getRouteContext (). render ( "files" );
}
@ GET ( "/download" )
public File download () {
// send a file as response
return new File ( "pom.xml" );
}
@ POST ( "/upload" )
@ Produces ( Produces . TEXT )
public String upload ( FileItem file ) {
// send a text (the info about uploaded file) as response
// return file.toString();
return new StringBuilder ()
. append ( file . getName ()). append ( " n " )
. append ( file . getSubmittedFileName ()). append ( " n " )
. append ( file . getSize ()). append ( " n " )
. append ( file . getContentType ())
. toString ();
}
}在您的應用程序中添加控制器:
public class BasicApplication extends ControllerApplication {
@ Override
protected void onInit () {
addControllers ( ContactsController . class ); // one instance for EACH request
// OR
addControllers ( new ContactsController ()); // one instance for ALL requests
addControllers ( FilesController . class );
}
}不要忘記, Controller概念包含在pippo-controller模塊中,因此您必須將此模塊添加為項目中的依賴項。
可以在Pippo.ro上找到文檔
演示應用程序可在Pippo-Demo上獲得
有關使用Pippo構建的現實生活應用程序,請查看Web會計 - Pippo演示