使用Java的RESTful Web服务
生活随笔
收集整理的這篇文章主要介紹了
使用Java的RESTful Web服务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
REST代表“代表性狀態轉移”,由Roy Fielding于2000年在其論文“建筑風格和基于網絡的軟件體系結構設計”中首次提出。
REST是一種建筑風格。 HTTP是一種協議,其中包含一組REST體系結構約束。
REST基礎
- REST中的所有內容都被視為資源。
- 每個資源都由URI標識。
- 使用統一的接口。 使用POST,GET,PUT,DELETE操作處理資源,這些操作類似于創建,讀取,更新和刪除(CRUD)操作。
- 無國籍。 每個請求都是一個獨立的請求。 從客戶端到服務器的每個請求必須包含理解該請求所需的所有信息。
- 通信通過表示進行。 例如XML,JSON
RESTful Web服務
RESTful Web Services因其簡單性而被Web上的大型服務提供商所接受,作為基于SOAP的Web Services的替代方案。 這篇文章將演示如何使用擴展JAX-RS API的Jersey框架創建RESTful Web服務和客戶端。 使用Eclipse IDE和Java SE 6完成了示例。
- 在Eclipse中,創建一個名為“ RESTfulWS”的新動態Web項目。
- 從此處下載Jersey zip捆綁包。 這些示例中使用的Jersey版本是1.17.1。 解壓縮后,將有一個名為“ jersey-archive-1.17.1”的目錄。 在其中找到lib目錄。 從那里復制以下jar,并將其粘貼到項目中的WEB-INF-> lib文件夾中。 完成此操作后,也將這些jar添加到項目構建路徑中。
- 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”的新包。 在其中創建一個新的Java類,稱為“ UserInfo”。 還將給定的web.xml文件包含在WEB-INF文件夾中。
UserInfo.java
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 here defines class level path. Identifies the URI path that // a resource class will serve requests for. @Path("UserInfoService") public class UserInfo {// @GET here defines, this method will method will process HTTP GET// requests.@GET// @Path here defines method level path. Identifies the URI path that a// resource class method will serve requests for.@Path("/name/{i}")// @Produces here defines the media type(s) that the methods// of a resource class can produce.@Produces(MediaType.TEXT_XML)// @PathParam injects the value of URI parameter that defined in @Path// expression, into the method.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
<?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 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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”的Java類。
UserInfoClient.java
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));}/*** Returns client response.* e.g : * GET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra * returned a response status of 200 OK** @param service* @return*/private static String getClientResponse(WebResource resource) {return resource.accept(MediaType.TEXT_XML).get(ClientResponse.class).toString();}/*** Returns the response as XML* e.g : <User><Name>Pavithra</Name></User> * * @param service* @return*/private static String getResponse(WebResource resource) {return resource.accept(MediaType.TEXT_XML).get(String.class);} }- 運行客戶端程序后,將獲得以下輸出。
請享用!
翻譯自: https://www.javacodegeeks.com/2013/11/restful-web-services-with-java.html
總結
以上是生活随笔為你收集整理的使用Java的RESTful Web服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何更换路由器密码如何转移路由器账号密码
- 下一篇: ThreadLocal如何实现?