返回顶部

收藏

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. Neeke 发表 2013-01-22 05:22:43 ExtJS4 MVC CURD之查询列表篇
  5. Haacked 发表 2012-10-03 02:59:00 The ASP.NET MVC 4 Book Is Available!
  6. 夕水溪下 发表 2013-06-24 05:11:30 Spring 3 mvc hello world例子
  7. itcodes 发表 2013-10-25 01:43:53 ASP.NET MVC入门(1)
  8. Neeke 发表 2013-06-04 08:11:42 Spring MVC 3.2 DeferredResult and long polling
  9. ToFishes 发表 2012-04-12 10:49:07 spring3.1.1 mvc使用注解搭建hello world
  10. youngsterxyf 发表 2014-11-08 16:00:00 读文笔记:An Introduction to APIs
  11. 逝去日子 发表 2015-01-28 14:21:05 asp.net MVC使用EF6连接mysql
  12. LI Daobing 发表 2011-03-04 08:00:00 [评论] REST 的缺点

发表评论