返回顶部

收藏

SerfJ REST框架的示例代码

更多

SerfJ 是一个最简单的框架用来开发Java的REST的Web应用。可帮助你开发优雅的MVC架构的应用,使用惯例重于配置的思路,无需配置文件和注解。

web.xml

<servlet>
    <servlet-name>RestServlet</servlet-name>
    <servlet-class>net.sf.serfj.RestServlet</servlet-class>
    <load-on-startup>5</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>RestServlet</servlet-name>
    <url-pattern>/banks/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>RestServlet</servlet-name>
    <url-pattern>/accounts/*</url-pattern>
</servlet-mapping>

serfj.properties

# Main package where looking for classes (controllers, serializers)
main.package=net.sf.serfj.test

Bank.java

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("bank");

        // Gets account's ID from URL /banks/1/accounts/2
        String accountId = this.getId("account");

        // 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)
    }

    @PUT
    public void update() {
        // Gets bank's ID
        String id = this.getId("bank");

        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.java

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() {
        // 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);
    }
}

标签:rest,MVC,SerfJ

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. H.E. 发表 2010-04-20 07:28:08 Spring3 REST MVC框架,提速你的Web开发
  2. H.E. 发表 2010-04-22 07:28:28 Spring3文件上传,提速你的Web开发
  3. H.E. 发表 2010-07-27 03:41:56 Spring3 REST 上传多个文件
  4. 博主 发表 2013-03-21 21:28:25 Amazon S3 REST API详解
  5. Neeke 发表 2013-06-04 08:11:42 Spring MVC 3.2 DeferredResult and long polling
  6. 博主 发表 2013-08-06 17:45:41 为啥REST如此重要?
  7. 博主 发表 2013-11-18 08:00:00 Rest Response and Exception
  8. 博主 发表 2014-04-06 02:49:35 如何定义一个好的REST接口3
  9. 博主 发表 2014-08-10 10:38:00 iOS 的 MVC 以及 View Controller 间的切换
  10. neo 发表 2013-05-18 15:18:52 PHP语言访问REST API上传图片的解决方案
  11. 博主 发表 2010-06-09 21:08:58 ASP.NET MVC 2 中TempData的生命周期
  12. otakustay 发表 2011-12-08 04:00:00 另类MVC模式 - 思考和雏形

发表评论