Java 的 REST 框架,Serfj 0.4.0 發布
SerfJ 是一個最簡單的框架用來開發Java的REST的Web應用。可幫助你開發優雅的MVC架構的應用,使用慣例重于配置的思路,無需配置文件和注解。
Serfj 0.4.0 發布了,新版本改進記錄包括:
- Feature: Default FileSerializer implementation, now is possible to serve files for downloading.
- Feature: New default extension (.file) for serving files.
- Feature: Within functional style you are able to implement generic serializers for whatever model you have.
- Feature: Now javax.servlet.ServletContext, javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse are accessible from controllers.
- Patch: Instead of implementing net.sf.serfj.serializers.Serializer developers must implement net.sf.serfj.serializers.ObjectSerializer for serializing objects.
- Patch: RestController.addObject2request is deprecated in favour of RestController.putParam
public class Bank extends RestController { @GET public void index() { // By default, this action redirects to index.jsp (or index.html or index.htm) } @GET public void show() { // Gets ID from URL /banks/1 String id = this.getId(); // By default, this action redirects to show.jsp (or show.html or show.htm) } @GET public void newResource() { // By default, this action redirects to new.jsp (or new.html or new.htm) } @GET public void edit() { // By default, this action redirects to edit.jsp (or edit.html or edit.htm) } @POST public void create() { // By default, this action redirects to create.jsp (or create.html or create.htm) } @PUT public void update() { // Gets bank's ID String id = this.getId("bank"); // ... or another way to get the main Id String bankId = this.getId(); Bank bank = // Code that gets the bank object // Gets new name for the bank String name = this.getStringParam("name"); // Updating the bank // ... Code that updates the bank's information // By default, this action redirects to update.jsp (or update.html or update.htm) } @DELETE public void delete() { // By default, this action redirects to delete.jsp (or delete.html or delete.htm) } @GET public void someAction() { // By default, this action redirects to someAction.jsp (or someAction.html or someAction.htm) } } Account's controller: public class Account extends RestController { @GET public void index() { // By default, this action redirects to index.jsp (or index.html or index.htm) } @GET public void show() { // Gets account's ID from URL /banks/1/accounts/2 String accountId = this.getId("account"); // Gets account's ID from URL /banks/1/accounts/2 String theSameAccountId = this.getId(); // Gets bank's ID from URL /banks/1/accounts/2 String bankId = this.getId("bank"); // Gets the account Account account = // Code that gets the account 2 from bank 1 // Put account into the request so the page will be able to use it this.addObject2Request("account", account); // By default, this action redirects to show.jsp (or show.html or show.htm) } @GET public void newResource() { // By default, this action redirects to new.jsp (or new.html or new.htm) } @GET public void edit() { // By default, this action redirects to edit.jsp (or edit.html or edit.htm) } @POST public void create() { // By default, this action redirects to create.jsp (or create.html or create.htm) // But I want to render another page!... easy this.renderPage("mypage.jsp"); } @PUT public void update() { // By default, this action redirects to update.jsp (or update.html or update.htm) // But I want to render another page... from another controller!... easy this.renderPage("bank", "another_page.jsp"); } @DELETE public void delete() { // By default, this action redirects to delete.jsp (or delete.html or delete.htm) // Well, if something happens, I want to redirect to mypage.jsp if (somethingHappens) { this.renderPage("mypage.jsp"); } else { // Default page this.renderPage(); } } /** * If this method is called as /accounts/1/accountBalance.xml, then the balance object will * be serialized as an XML, whereas if it's called as /accounts/1/accountBalance.json, the * object will be serialized as a JSON object. */ @POST public Balance accountBalance() { // Gets account's Id String id = this.getId("account"); // Calculate balance BalanceManager manager = new BalanceManager(); Balance balance = manager.getBalance(id); this.serialize(balance); } }
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!