日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Pippo java微服务,轻量级web开发框架,原来Filter还能这么玩

發(fā)布時(shí)間:2024/3/12 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pippo java微服务,轻量级web开发框架,原来Filter还能这么玩 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Filter - > ANY? ,? GET -> GET ,POST -> POST 極致精簡(jiǎn)

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.

1.1 Routes approach

Add some routes in your application:

public class BasicApplication extends Application {@Overrideprotected void onInit() {// send 'Hello World' as responseGET("/", routeContext -> routeContext.send("Hello World"));// send a file as responseGET("/file", routeContext -> routeContext.send(new File("pom.xml")));// send a json as responseGET("/json", routeContext -> {Contact contact = createContact();routeContext.json().send(contact);});// send xml as responseGET("/xml", routeContext -> {Contact contact = createContact();routeContext.xml().send(contact);});// send an object and negotiate the Response content-type, default to XMLGET("/negotiate", routeContext -> {Contact contact = 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");});}private Contact createContact() {return new Contact().setId(12345).setName("John").setPhone("0733434435").setAddress("Sunflower Street, No. 6");}}

1.2 Controllers approach

Define controller(s):

@Path("/contacts") @Logging public class ContactsController extends Controller {private ContactService contactService;public ContactsController() {contactService = new InMemoryContactService();}@GET@Named("index") // @Produces(Produces.HTML)@Metered@Loggingpublic void index() {// 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)@Timedpublic 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)@NoCachepublic 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 {@GETpublic void index() {// send a template with name "files" as responsegetRouteContext().render("files");}@GET("/download")public File download() {// send a file as responsereturn 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();}}

Add controller(s) in your application:

public class BasicApplication extends ControllerApplication {@Overrideprotected void onInit() {addControllers(ContactsController.class); // one instance for EACH request// ORaddControllers(new ContactsController()); // one instance for ALL requestsaddControllers(FilesController.class);}}

2. Start your application

public class BasicDemo {public static void main(String[] args) {Pippo pippo = new Pippo(new BasicApplication());pippo.start();}}

原文地址:http://www.pippo.ro/

?

總結(jié)

以上是生活随笔為你收集整理的Pippo java微服务,轻量级web开发框架,原来Filter还能这么玩的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。