From: http://www.oschina.net/code/snippet_12_1948
SerfJ 是一个比较简单的框架用来开发Java的REST的Web应用。可帮助你开发优雅的MVC架构的应用,使用惯例重于配置的思路,无需配置文件和注解。
SerfJ 框架的示例代码请看下面(代码分享):
1. [代码]web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<
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
>
|
2. [代码]serfj.properties
1
2
|
# Main package where looking for classes (controllers, serializers)
main.package=net.sf.serfj.
test
|
3. [代码]Bank.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
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)
}
}
|
4. [代码]Account.java 跳至 [1] [2] [3] [4] [全屏预览]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
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);
}
}
|