一
REST是REpresentational State Transfer的縮寫
RESTful Web Service與基於SOAP和WSDL的Web Service有著很多的不同
·將Web Service作為一種資源
·使用HTTP中的POST
·使用無狀態通信
·傳輸XML或者SON
在JAX
對RESTful Web Service提供完整支持的JAX
二
本文基於WAS CE的最新版本V
·Sun JDK V
·Eclipse IDE for Java EE Developers
·WASCE Eclipse Plug
此外
去掉以下四個module的condition屬性
<module name=
<module name=
<module name=
<module name=
增加load屬性
<module name=
<module name=
<module name=
<module name=
三
選擇File
輸入Project Name為HelloRestfulService
package com
import java
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import org
import org
import org
@WebServiceProvider
@BindingType (value = HTTPBinding
public class HelloWorld implements Provider<Source> {
@Resource
protected WebServiceContext wsContext ;
public Source invoke(Source source) {
try {
String targetName = null ;
if (source == null ) {
//Get: Getting input from query string
MessageContext mc = wsContext
String query = (String) mc
System
ServletRequest req = (ServletRequest) mc
targetName = req
} else {
//POST: Getting input from input box
Node n = null ;
if (source instanceof DOMSource) {
n = ((DOMSource) source)
} else if (source instanceof StreamSource) {
StreamSource streamSource = (StreamSource) source;
DocumentBuilder builder = DocumentBuilderFactory
InputSource inputSource = null ;
if (streamSource
inputSource = new InputSource(streamSource
} else if (streamSource
inputSource = new InputSource(streamSource
}
n = builder
} else {
throw new RuntimeException(
}
NodeList children = n
for ( int i =
Node child = em(i);
if (child
targetName = child
break ;
}
}
}
String body =
+
+
return new StreamSource( new ByteArrayInputStream(body
} catch (Exception e) {
e
throw new HTTPException(
}
}
private String sayHello(String target){
return
}
}
讓我們看一看代碼中的幾個關鍵點
a) @WebServiceProvider 表明這個Web Service實現了Provider接口
b) Provider 是這類Web Service都要實現的接口
public abstractjava
c) Source 是交換信息的載體
當Source對象為空時
否則
另外
為了使我們前面編寫的Web Service能夠成功部署到WAS CE中
<servlet>
<servlet
<servlet
</servlet>
<servlet
<servlet
<url
</servlet
注意
右擊這個HelloRestfulService工程
通過訪問如下地址
//localhost
四
選擇File
輸入Project Name為HelloRestfulClient
<form method=
Target Name: <input type=
<input type=
</form>
這個JSP用來為HelloGetMethodRequester Servlet提供參數
protected void doPost(HttpServletRequest request
PrintWriter ut = response
String target = request
String queryRequest = //localhost:
GetMethod method = new GetMethod(queryRequest);
HttpClient client = new HttpClient();
int statusCode = client
if (statusCode !=
System
}
try {
DocumentBuilder builder= DocumentBuilderFactory
Document queryResponse = builder
XPath xPath = XPathFactory
NodeList nodes = (NodeList) xPath
for ( int i =
// Get eachxpathexpression as a string
String str = (String) xPath
out
}
} catch (Exception e) {
e
}
}
在這個Servlet中我們用到了commons
這兩個包在WAS CE的如下目錄中可以找到
<WASCE_HOME>\repository\commons
<WASCE_HOME >\repository\commons
讓我們看一看這段Servlet代碼中的一些關鍵點
a) 首先創建了一個HttpClient對象
//localhost
b) 如果成功返回
method
c) 因為返回的結果為自定義的一段XML文檔
為使這個Web Client能夠成功部署到WAS CE中
<dep:dependencies>
<dep:dependency>
<dep:groupId>commons
<dep:artifactId>commons
<dep:version>
<dep:type>jar</dep:type>
</dep:dependency>
<dep:dependency>
<dep:groupId>commons
<dep:artifactId>commons
<dep:version>
<dep:type>jar</dep:type>
</dep:dependency>
</dep:dependencies>
右擊這個HelloRestfulClient工程
在浏覽器中打開如下頁面
輸入
五
本文介紹了REST的基本概念
事實上
六
·WAS CE及Samples下載
·WAS CE Eclipse Plug
·WAS CE文檔
·WAS CE主頁
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28821.html