package com.demo;
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供河西網(wǎng)站建設(shè)、河西做網(wǎng)站、河西網(wǎng)站設(shè)計、河西網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、河西企業(yè)網(wǎng)站模板建站服務(wù),十多年河西做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
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實現(xiàn)了restfull標(biāo)準(zhǔn)的api,基于jsr-311,sun自己實現(xiàn)了jersey
不過不要高興太早,jersey只姿斗茄是一個restful的api,不是rest的跡察,要實現(xiàn)rest非常復(fù)雜,因為涉銷碼及到了超文本驅(qū)動這個。
我建議你看看jersey + spring3來玩rest
下圖顯示了示例實現(xiàn)中的類。藍(lán)色所示的類是框架外部的類,將它們放在這里是為了展示與框架的結(jié)構(gòu)關(guān)系。
配置文件
咐猜 配置文件 "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
在該示例實現(xiàn)中,XML Binding服務(wù)實現(xiàn)在"rl-config.xml"文件中配置的框架配置文件如下所示。通過修改此文件實現(xiàn)的任何自定義實現(xiàn)都可以接入,只要實現(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中配置,處理所有具有上下文路徑的請求,其中上下文路徑的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
對于每個REST資源,例如 GetUserProfile,都將創(chuàng)建一個實現(xiàn)ActionInterface的相應(yīng)動作類。該接口定義了動作類需要實運簡緩現(xiàn)的 "doExecute(ActionContext ctx)" 方法。ActionContext提供服務(wù),獲取 REST 路徑輸入或查詢參數(shù),獲取XMLBindingService實例并將XML輸出發(fā)送到客戶端,不公開Action的協(xié)議細(xì)節(jié)。PathInputs是一個包含路徑值的List對象,路徑值的順序與它們在URL中指定的順序相同。
清單 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);
}
動作類負(fù)責(zé)使用超類中的XMLBindingService以XML形式生成輸出。請查看示例實現(xiàn)的ws.registration.restactions.GetProfile類。ActionContext還可以提供協(xié)議特定的HttpServletRequest和HttpServletResponse對象,以防需要自定義處理。它還提供了Path值和URL參數(shù)。
XML Binding
該代碼示例提供了一個Java XML Binding的實現(xiàn),該實現(xiàn)使用java.beans.XMLEncoder和java.beans.XMLDecoder類。XML Binding服務(wù)實現(xiàn)接受一個JavaBean對象,并將其轉(zhuǎn)換為上述Encoder和Decoder相應(yīng)的XML表示形式。如果需要JAXB實現(xiàn),那么可以開發(fā)一個實現(xiàn) ws.rest.xmlbinding.service.XMLBindingService接口的實現(xiàn)類。
執(zhí)行示例服務(wù)
示例代碼分發(fā)包含示例WAR文件"RESTWS.war",它可以部署在Tomcat容器中(已在Apache Tomcat版本6.0.20上進行了測試)。JDK要求是JDK 1.5以上。
成功部署該應(yīng)用程序之后,在瀏覽器中輸入URL:
圖 5. 創(chuàng)建Profile Service輸入
該頁面調(diào)用REST服務(wù)
POST url-prefix/Registration/CreateProfile
您可以修改在 string/string 標(biāo)記中指定的XML值。
注意:請注意XML結(jié)構(gòu)依賴于JavaBean對象和Java使用的XML序列化技術(shù)。
提交時,動作類顯示成功消息,表示后端服務(wù)的調(diào)用。可以查看 ws_log.log 文件調(diào)試消息。
圖 6. 創(chuàng)建Profile Service輸出
類似地,實現(xiàn)示例GET url-prefix/Registration/GetProfile/{username}服務(wù)以檢索配置文件,如下圖所示:
轉(zhuǎn)載僅供參考,版權(quán)屬于原作者。祝你愉快,滿意請采納哦
java動態(tài)修改rest路徑的方法如下:
1、使用Servlet規(guī)范:Java中的REST服務(wù)通常使用Servlet作為其請求處理器,可以通過修改其Servlet映射路徑來修改REST服務(wù)的URL路徑。
2、使用反向伏前旅悔賣代理:可以在REST服務(wù)和客戶端之間添加一個缺凳反向代理,通過修改反向代理的URL路徑來修改REST服務(wù)的URL路徑。