下圖顯示了示例實(shí)現(xiàn)中的類(lèi)。藍(lán)色所示的類(lèi)是框架外部的類(lèi),將它們放在這里是為了展示與框架的結(jié)構(gòu)關(guān)系。

成都創(chuàng)新互聯(lián)公司是專(zhuān)業(yè)的和平網(wǎng)站建設(shè)公司,和平接單;提供成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行和平網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
配置文件
配置文件 "rest-services-config.xml" 包含 REST 服務(wù)表示形式和相應(yīng)的Java Action之間的映射,如下:
清單 1. REST 服務(wù)配置
以下是引用片段:
?xml version="1.0" ?
rest-config
rest-api id="CreateUserProfile" uri="/Registration/CreateUser" method="POST"
handler id="RegAction" class="ws.registration.restactions.CreateProfile"/
/rest-api
rest-api id="GetUserProfile" uri="/Registration/GetUser" method="GET"
handler id="RegAction" class=" ws.registration.restactions.GetProfile"/
/rest-api
...
/rest-config
在該示例實(shí)現(xiàn)中,XML Binding服務(wù)實(shí)現(xiàn)在"rl-config.xml"文件中配置的框架配置文件如下所示。通過(guò)修改此文件實(shí)現(xiàn)的任何自定義實(shí)現(xiàn)都可以接入,只要實(shí)現(xiàn)了XMLBindingService接口。
清單 2:框架配置
以下是引用片段:
# XML Binding Implementation Service
# Default implementation
ws.rest.xmlbinding.service.impl=ws.rest.xmlbinding.service.impl.XMLEncDecServiceImpl
日志配置文件 "ws_log.properties" 指定log4j屬性和日志文件的位置。這可以按需要作出適當(dāng)修改。
Controller Servlet
RESTServiceServlet在web.xml中配置,處理所有具有上下文路徑的請(qǐng)求,其中上下文路徑的web-app/restservices/*如下所示:
清單 3:Servlet配置
以下是引用片段:
servlet
description/description
display-nameRESTServletService/display-name
servlet-nameRESTServletService/servlet-name
servlet-classws.rest.servlet.RESTServiceServlet/servlet-class
/servlet
servlet-mapping
servlet-nameRESTServletService/servlet-name
url-pattern/restservices/*/url-pattern
/servlet-mapping
REST Action
對(duì)于每個(gè)REST資源,例如 GetUserProfile,都將創(chuàng)建一個(gè)實(shí)現(xiàn)ActionInterface的相應(yīng)動(dòng)作類(lèi)。該接口定義了動(dòng)作類(lèi)需要實(shí)現(xiàn)的 "doExecute(ActionContext ctx)" 方法。ActionContext提供服務(wù),獲取 REST 路徑輸入或查詢(xún)參數(shù),獲取XMLBindingService實(shí)例并將XML輸出發(fā)送到客戶端,不公開(kāi)Action的協(xié)議細(xì)節(jié)。PathInputs是一個(gè)包含路徑值的List對(duì)象,路徑值的順序與它們?cè)赨RL中指定的順序相同。
清單 4:Action代碼片段
以下是引用片段:
public class GetProfile implements ActionInterface {
public void doExecute(ActionContext context) throws Exception {
// Get the value from URL path
String userName = context.getPathInputs().get(0);
// Invoke backend service to retrieve user profile
UserProfileBean bean = getUser(userName);
// Serialize the bean using framework service and send response
String xml = context.getXMLBindingService().serialize(bean);
// Use the ActionContext to generate XML and
context.sendResponse(response, xml);
}
動(dòng)作類(lèi)負(fù)責(zé)使用超類(lèi)中的XMLBindingService以XML形式生成輸出。請(qǐng)查看示例實(shí)現(xiàn)的ws.registration.restactions.GetProfile類(lèi)。ActionContext還可以提供協(xié)議特定的HttpServletRequest和HttpServletResponse對(duì)象,以防需要自定義處理。它還提供了Path值和URL參數(shù)。
XML Binding
該代碼示例提供了一個(gè)Java XML Binding的實(shí)現(xiàn),該實(shí)現(xiàn)使用java.beans.XMLEncoder和java.beans.XMLDecoder類(lèi)。XML Binding服務(wù)實(shí)現(xiàn)接受一個(gè)JavaBean對(duì)象,并將其轉(zhuǎn)換為上述Encoder和Decoder相應(yīng)的XML表示形式。如果需要JAXB實(shí)現(xiàn),那么可以開(kāi)發(fā)一個(gè)實(shí)現(xiàn) ws.rest.xmlbinding.service.XMLBindingService接口的實(shí)現(xiàn)類(lèi)。
執(zhí)行示例服務(wù)
示例代碼分發(fā)包含示例WAR文件"RESTWS.war",它可以部署在Tomcat容器中(已在Apache Tomcat版本6.0.20上進(jìn)行了測(cè)試)。JDK要求是JDK 1.5以上。
成功部署該應(yīng)用程序之后,在瀏覽器中輸入U(xiǎn)RL:
圖 5. 創(chuàng)建Profile Service輸入
該頁(yè)面調(diào)用REST服務(wù)
POST url-prefix/Registration/CreateProfile
您可以修改在 string/string 標(biāo)記中指定的XML值。
注意:請(qǐng)注意XML結(jié)構(gòu)依賴(lài)于JavaBean對(duì)象和Java使用的XML序列化技術(shù)。
提交時(shí),動(dòng)作類(lèi)顯示成功消息,表示后端服務(wù)的調(diào)用。可以查看 ws_log.log 文件調(diào)試消息。
圖 6. 創(chuàng)建Profile Service輸出
類(lèi)似地,實(shí)現(xiàn)示例GET url-prefix/Registration/GetProfile/{username}服務(wù)以檢索配置文件,如下圖所示:
轉(zhuǎn)載僅供參考,版權(quán)屬于原作者。祝你愉快,滿意請(qǐng)采納哦
package com.demo;
import jaimg id="selectsearch-icon" src="" alt="搜索"va.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.xml.bind.DatatypeConverter;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class restTest {
public static voidmain(String[] args) {
try {
DefaultHttpClient Client = newDefaultHttpClient();
HttpGet httpGet = newHttpGet("你的地址");
String encoding =DatatypeConverter.printBase64Binary("admin:admin".getBytes("UTF-8"));
httpGet.setHeader("Authorization", "Basic " +encoding);
HttpResponse response = Client.execute(httpGet);
System.out.println("response =" + response);
BufferedReader breader = newBufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder responseString = newStringBuilder();
String line = "";
while ((line = breader.readLine()) !=null) {
responseString.append(line);
}
breader.close();
String repsonseStr =responseString.toString();
System.out.println("repsonseStr =" + repsonseStr);
} catch (IOException e) {
e.printStackTrace();
}
}
}
jsr-311實(shí)現(xiàn)了restfull標(biāo)準(zhǔn)的api,基于jsr-311,sun自己實(shí)現(xiàn)了jersey
不過(guò)不要高興太早,jersey只是一個(gè)restful的api,不是rest的,要實(shí)現(xiàn)rest非常復(fù)雜,因?yàn)樯婕暗搅顺谋掘?qū)動(dòng)這個(gè)。
我建議你看看jersey + spring3來(lái)玩rest
網(wǎng)頁(yè)標(biāo)題:restjava代碼,java rest api開(kāi)發(fā)
網(wǎng)站路徑:http://chinadenli.net/article26/hsjecg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、App開(kāi)發(fā)、網(wǎng)站排名、服務(wù)器托管、外貿(mào)建站、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)