REST是REpresentational State Transfer的縮寫(一般中文翻譯為表述性狀態轉移)。2000年Roy Fielding博士在他的博士論文“Architectural Styles and the Design of Network-based Software Architectures”《體系結構與基於網絡的軟件架構設計》中提出了REST。
REST是一種體系結構。而HTTP是一種包含了REST架構屬性的協議。
REST基礎概念
- 在REST中所有東西都被看作資源。每一個資源都有一個URI和它對應。
- 在REST中使用統一接口處理資源。與數據庫CRUD操作(Create、Read、Update 和 Delete)一樣,可以用POST、GET、PUT和DELETE處理REST資源。
- 每個REST請求都是孤立的,請求中包含了所需的全部信息。REST服務端不存儲狀態。
- REST支持不同的通信數據格式,比如XML、JSON。
RESTful Web Services
RESTful Web Services因其簡單性被廣泛使用,它比SOAP要更簡單。本文將重點介紹如何使用Jersey框架創建RESTful Web Services。Jersey框架實現了JAX-RS接口。本文示例代碼使用Eclipse和Java SE 6編寫。
創建RESTful Web Service服務端
- 在Eclipse中創建一個“dynamic web project”(動態web工程) ,項目名設為 “RESTfulWS”。
- 從這里下載Jersey。示例代碼使用的是Jersey 1.17.1。首先解壓Jersey到“jersey-archive-1.17.1”文件夾。接着將里面lib文件夾下的jar文件拷貝到工程目錄的WEB-INF -> lib。然后將它們添加到build path。
- asm-3.1.jar
- jersey-client-1.17.1.jar
- jersey-core-1.17.1.jar
- jersey-server-1.17.1.jar
- jersey-servlet-1.17.1.jar
- jsr311-api-1.1.1.jar
- 在工程Java Resources -> src中創建“com.eviac.blog.restws”包,並在其中創建“UserInfo”類。最后把web.xml拷貝到WEB-INF目錄下。
UserInfo.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
|
package
com.eviac.blog.restws;
import
javax.ws.rs.GET;
import
javax.ws.rs.Path;
import
javax.ws.rs.PathParam;
import
javax.ws.rs.Produces;
import
javax.ws.rs.core.MediaType;
/**
*
* @author pavithra
*
*/
// 這里@Path定義了類的層次路徑。
// 指定了資源類提供服務的URI路徑。
@Path
(
"UserInfoService"
)
public
class
UserInfo {
// @GET表示方法會處理HTTP GET請求
@GET
// 這里@Path定義了類的層次路徑。指定了資源類提供服務的URI路徑。
@Path
(
"/name/{i}"
)
// @Produces定義了資源類方法會生成的媒體類型。
@Produces
(MediaType.TEXT_XML)
// @PathParam向@Path定義的表達式注入URI參數值。
public
String userName(
@PathParam
(
"i"
) String i) {
String name = i;
return
"<User>"
+
"<Name>"
+ name +
"</Name>"
+
"</User>"
;
}
@GET
@Path
(
"/age/{j}"
)
@Produces
(MediaType.TEXT_XML)
public
String userAge(
@PathParam
(
"j"
)
int
j) {
int
age = j;
return
"<User>"
+
"<Age>"
+ age +
"</Age>"
+
"</User>"
;
}
}
|
web.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:web
=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee <a href="
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</
a
> id="WebApp_ID" version="2.5">
<
display-name
>RESTfulWS</
display-name
>
<
servlet
>
<
servlet-name
>Jersey REST Service</
servlet-name
>
<
servlet-class
>com.sun.jersey.spi.container.servlet.ServletContainer</
servlet-class
>
<
init-param
>
<
param-name
>com.sun.jersey.config.property.packages</
param-name
>
<
param-value
>com.eviac.blog.restws</
param-value
>
</
init-param
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>Jersey REST Service</
servlet-name
>
<
url-pattern
>/rest/*</
url-pattern
>
</
servlet-mapping
>
</
web-app
>
|
- 將此URL拷貝到瀏覽器地址欄中運行:
- http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra
輸出結果如下:
創建客戶端
創建一個“com.eviac.blog.restclient”包,然后新建“UserInfoClient”類。
UserInfoClient.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
65
66
|
package
com.eviac.blog.restclient;
import
javax.ws.rs.core.MediaType;
import
com.sun.jersey.api.client.Client;
import
com.sun.jersey.api.client.ClientResponse;
import
com.sun.jersey.api.client.WebResource;
import
com.sun.jersey.api.client.config.ClientConfig;
import
com.sun.jersey.api.client.config.DefaultClientConfig;
/**
*
* @author pavithra
*
*/
public
class
UserInfoClient {
public
static
final
String BASE_URI =
"http://localhost:8080/RESTfulWS"
;
public
static
final
String PATH_NAME =
"/UserInfoService/name/"
;
public
static
final
String PATH_AGE =
"/UserInfoService/age/"
;
public
static
void
main(String[] args) {
String name =
"Pavithra"
;
int
age =
25
;
ClientConfig config =
new
DefaultClientConfig();
Client client = Client.create(config);
WebResource resource = client.resource(BASE_URI);
WebResource nameResource = resource.path(
"rest"
).path(PATH_NAME + name);
System.out.println(
"Client Response \n"
+ getClientResponse(nameResource));
System.out.println(
"Response \n"
+ getResponse(nameResource) +
"\n\n"
);
WebResource ageResource = resource.path(
"rest"
).path(PATH_AGE + age);
System.out.println(
"Client Response \n"
+ getClientResponse(ageResource));
System.out.println(
"Response \n"
+ getResponse(ageResource));
}
/**
* 返回客戶端請求。
* 例如:
* GET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra
* 返回請求結果狀態“200 OK”。
*
* @param service
* @return
*/
private
static
String getClientResponse(WebResource resource) {
return
resource.accept(MediaType.TEXT_XML).get(ClientResponse.
class
)
.toString();
}
/**
* 返回請求結果XML
* 例如:<User><Name>Pavithra</Name></User>
*
* @param service
* @return
*/
private
static
String getResponse(WebResource resource) {
return
resource.accept(MediaType.TEXT_XML).get(String.
class
);
}
}
|
- 運行客戶端程序后,可以看到以下輸出:
|
1
2
3
4
5
6
7
8
9
|
Client Response
GET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra returned a response status of 200 OK
Response
<
User
><
Name
>Pavithra</
Name
></
User
>
Client Response
GET http://localhost:8080/RESTfulWS/rest/UserInfoService/age/25 returned a response status of 200 OK
Response
<
User
><
Age
>25</
Age
></
User
>
|
試試吧 :)
http://www.importnew.com/7336.html





