REST框架SerfJ
来源:互联网 发布:money理财通mac版破解 编辑:程序博客网 时间:2024/05/21 07:08
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);
}
}
举报
0 0
- REST框架SerfJ
- Java的REST框架 SerfJ
- SerfJ REST框架的示例代码
- SerfJ REST代码实例
- SerfJ-0.4.1 REST服务器、Mybatis3.2.3 持久框架、Log4j-1.2.17 日志整合框架
- IT忍者神龟之serfj开源框架
- rest框架
- REST开发框架纵览
- REST开发框架纵览
- Java REST框架一览
- REST框架的理解
- Java REST框架一览
- Java REST框架介绍
- Django REST框架实现
- Java REST框架一览
- Java REST框架一览
- Java REST框架一览
- Java REST框架一览
- 网格控件群组的高级使用
- c#中字段和属性的区别
- linux随笔(11)---umask和chmod
- 学习总结(C++课程感受)+目标
- 数据结构之顺序栈
- REST框架SerfJ
- Ubuntu 14.04 desktop 不能安装 openssh-server解决方法
- Fragment入门
- 请求转发与重定向(服务器与客户端跳转)
- 老生常谈:struct2文件下载(带点击取消出现的异常解决办法)
- 第一次作业
- 四、Linux/UNIX操作命令积累【chmod、chown、tail】
- UVAlive 6560 - The Urge to Merge(状压dp)
- ubuntu下python基础学习一