มันเป็นเฟรมเวิร์กเว็บโอเพนซอร์ส
เป้าหมายของโครงการนี้คือการสร้างกรอบเว็บขนาดเล็กใน Java ที่ควรใช้งานง่ายและแฮ็ค
ขนาดของ 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 และ JAXB เป็นเอ็นจิ้น XML
maven maven 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 เปิดตัวเว็บเซิร์ฟเวอร์ฝังตัว (พบใน ClassPath ของคุณ) และทำให้แอปพลิเคชันพร้อมใช้งานบนพอร์ต 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 โปรดดูที่บัญชีเว็บ - Pippo Demo