SerfJ REST代码实例
来源:互联网 发布:2015网络流行语大全 编辑:程序博客网 时间:2024/05/22 02:18
SerfJ 是一个最简单的框架用来开发Java的REST的Web应用。可帮助你开发优雅的MVC架构的应用,使用惯例重于配置的思路,无需配置文件和注解。
[代码] web.xml
01
<
servlet
>
02
<
servlet-name
>RestServlet</
servlet-name
>
03
<
servlet-class
>net.sf.serfj.RestServlet</
servlet-class
>
04
<
load-on-startup
>5</
load-on-startup
>
05
</
servlet
>
06
07
<
servlet-mapping
>
08
<
servlet-name
>RestServlet</
servlet-name
>
09
<
url-pattern
>/banks/*</
url-pattern
>
10
</
servlet-mapping
>
11
12
<
servlet-mapping
>
13
<
servlet-name
>RestServlet</
servlet-name
>
14
<
url-pattern
>/accounts/*</
url-pattern
>
15
</
servlet-mapping
>
[代码] serfj.properties
1
# Main package where looking for classes (controllers, serializers)
2
main.package=net.sf.serfj.
test
[代码] Bank.java
01
public
class
Bank
extends
RestController {
02
@GET
03
public
void
index() {
04
// By default, this action redirects to index.jsp (or index.html or index.htm)
05
}
06
07
@GET
08
public
void
show() {
09
// Gets ID from URL /banks/1
10
String id =
this
.getId(
"bank"
);
11
12
// Gets account's ID from URL /banks/1/accounts/2
13
String accountId =
this
.getId(
"account"
);
14
15
// Gets the account
16
Account account =
// Code that gets the account 2 from bank 1
17
18
// Put account into the request so the page will be able to use it
19
this
.addObject2Request(
"account"
, account);
20
21
// By default, this action redirects to show.jsp (or show.html or show.htm)
22
}
23
24
@GET
25
public
void
newResource() {
26
// By default, this action redirects to new.jsp (or new.html or new.htm)
27
}
28
29
@GET
30
public
void
edit() {
31
// By default, this action redirects to edit.jsp (or edit.html or edit.htm)
32
}
33
34
@POST
35
public
void
create() {
36
// By default, this action redirects to create.jsp (or create.html or create.htm)
37
}
38
39
@PUT
40
public
void
update() {
41
// Gets bank's ID
42
String id =
this
.getId(
"bank"
);
43
44
Bank bank =
// Code that gets the bank object
45
46
// Gets new name for the bank
47
String name =
this
.getStringParam(
"name"
);
48
49
// Updating the bank
50
// ... Code that updates the bank's information
51
52
// By default, this action redirects to update.jsp (or update.html or update.htm)
53
}
54
55
@DELETE
56
public
void
delete() {
57
// By default, this action redirects to delete.jsp (or delete.html or delete.htm)
58
}
59
60
@GET
61
public
void
someAction() {
62
// By default, this action redirects to someAction.jsp (or someAction.html or someAction.htm)
63
}
64
}
[代码] Account.java
01
public
class
Account
extends
RestController {
02
@GET
03
public
void
index() {
04
// By default, this action redirects to index.jsp (or index.html or index.htm)
05
}
06
07
@GET
08
public
void
show() {
09
// By default, this action redirects to show.jsp (or show.html or show.htm)
10
}
11
12
@GET
13
public
void
newResource() {
14
// By default, this action redirects to new.jsp (or new.html or new.htm)
15
}
16
17
@GET
18
public
void
edit() {
19
// By default, this action redirects to edit.jsp (or edit.html or edit.htm)
20
}
21
22
@POST
23
public
void
create() {
24
// By default, this action redirects to create.jsp (or create.html or create.htm)
25
// But I want to render another page!... easy
26
this
.renderPage(
"mypage.jsp"
);
27
}
28
29
@PUT
30
public
void
update() {
31
// By default, this action redirects to update.jsp (or update.html or update.htm)
32
// But I want to render another page... from another controller!... easy
33
this
.renderPage(
"bank"
,
"another_page.jsp"
);
34
}
35
36
@DELETE
37
public
void
delete() {
38
// By default, this action redirects to delete.jsp (or delete.html or delete.htm)
39
// Well, if something happens, I want to redirect to mypage.jsp
40
if
(somethingHappens) {
41
this
.renderPage(
"mypage.jsp"
);
42
}
else
{
43
// Default page
44
this
.renderPage();
45
}
46
}
47
48
/**
49
* If this method is called as /accounts/1/accountBalance.xml, then the balance object will
50
* be serialized as an XML, whereas if it's called as /accounts/1/accountBalance.json, the
51
* object will be serialized as a JSON object.
52
*/
53
@POST
54
public
Balance accountBalance() {
55
// Gets account's Id
56
String id =
this
.getId(
"account"
);
57
58
// Calculate balance
59
BalanceManager manager =
new
BalanceManager();
60
Balance balance = manager.getBalance(id);
61
this
.serialize(balance);
62
}
63
}
- SerfJ REST代码实例
- SerfJ REST框架的示例代码
- REST框架SerfJ
- Java的REST框架 SerfJ
- SerfJ-0.4.1 REST服务器、Mybatis3.2.3 持久框架、Log4j-1.2.17 日志整合框架
- springmvc rest风格的实例代码demo讲解
- springmvc rest风格的实例代码demo讲解
- springmvc rest风格的实例代码demo讲解
- springmvc rest风格的实例代码demo讲解
- jersey实现rest实例
- Serfj中文参考手册
- Struts2 支持REST 代码
- Spring3.0实现REST实例
- Spring3.0实现REST实例
- Spring3.0实现REST实例
- Jersey实现REST入门实例
- Spring3.0实现REST实例
- Spring3.0实现REST实例
- C++ I/O流
- flv视频播放器
- MyEclipse 安装Freemarker插件
- IOS ARC
- 将一个或多个空格替换成逗号“,”
- SerfJ REST代码实例
- .NET树形控件展开一个节点则关闭同级节点
- Lucene学习笔记
- object-c id用法
- 让我们用中文编程(二)之C++篇 - 自己做中文编程语言,你也行
- mysql 外键
- ant 安装
- Android WiFi Tethering issue
- pythonbrew学习笔记