Java 的 REST 框架,Serfj 0.4.0 发布

openkk 12年前
   <p><a href="/misc/goto?guid=4958522622127137223" target="_blank">SerfJ </a>是一个最简单的框架用来开发Java的REST的Web应用。可帮助你开发优雅的MVC架构的应用,使用惯例重于配置的思路,无需配置文件和注解。</p>    <p>Serfj 0.4.0 发布了,新版本改进记录包括:</p>    <ul>     <li>Feature: Default FileSerializer implementation, now is possible to serve files for downloading.</li>     <li>Feature: New default extension (.file) for serving files.</li>     <li>Feature: Within functional style you are able to implement generic serializers for whatever model you have.</li>     <li>Feature: Now javax.servlet.ServletContext, javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse are accessible from controllers.</li>     <li>Patch: Instead of implementing net.sf.serfj.serializers.Serializer developers must implement net.sf.serfj.serializers.ObjectSerializer for serializing objects.</li>     <li>Patch: RestController.addObject2request is deprecated in favour of RestController.putParam</li>    </ul>    <p></p>   <pre class="brush:java; toolbar: true; auto-links: false;">   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);         }     }          </pre>    <p></p>