Java의 오픈 소스 (Apache License) 마이크로 웹 프레임 워크이며 최소한의 종속성과 빠른 학습 곡선입니다.
이 프로젝트의 목표는 Java에서 사용하고 해킹하기가 쉬운 마이크로 웹 프레임 워크를 만드는 것입니다.
pippo-core 의 크기는 140kb 에 불과하며 pippo-controller (선택 사항)의 크기는 45kb 입니다.
먼저 응용 프로그램을 작성하고 일부 경로를 추가해야합니다.
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 엔진으로, XML 엔진으로 JAXB 선택합니다.
내 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 >마지막 단계는 애플리케이션 을 매개 변수로 시작하는 것입니다.
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