小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

用Jersey開發(fā)RESTful服務(wù)

 corefashion 2014-07-26

SOA專題

用Jersey開發(fā)RESTful服務(wù)

  REST基礎(chǔ)概念:

  • 在REST中的一切都被認(rèn)為是一種資源。
  • 每個(gè)資源由URI標(biāo)識。
  • 使用統(tǒng)一的接口。處理資源使用POST,GET,PUT,DELETE操作類似創(chuàng)建,讀取,更新和刪除(CRUD)操作。
  • 無狀態(tài)。每個(gè)請求是一個(gè)獨(dú)立的請求。從客戶端到服務(wù)器的每個(gè)請求都必須包含所有必要的信息,以便于理解。
  • 通信都是通過展現(xiàn)。例如XML,JSON

RESTful Web服務(wù)由于其簡單替代了基于SOAP的Web服務(wù),并大型服務(wù)提供商所接受。這篇文章使用Jersey框架延伸JAX-RS API將展示如何創(chuàng)建一個(gè)REST風(fēng)格的Web服務(wù)和客戶端。

 

在Eclipse中,創(chuàng)建一個(gè)新的動(dòng)態(tài)Web項(xiàng)目名為"RESTfulWS":

 

下載Jersey zip bundle 這里 here. 需要包:

  1. asm-3.1.jar
  2. jersey-client-1.17.1.jar
  3. jersey-core-1.17.1.jar
  4. jersey-server-1.17.1.jar
  5. jersey-servlet-1.17.1.jar
  6. jsr311-api-1.1.1.jar

jersey

加入項(xiàng)目:

 

創(chuàng)建Web服務(wù)類:

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./2001/XMLSchema-instance" xmlns="http://java./xml/ns/javaee" xmlns:web="http://java./xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java./xml/ns/javaee http://java./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>

點(diǎn)按這個(gè)項(xiàng)目 run as ->run on server.

瀏覽器瀏覽:http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra

URL說明:

 

創(chuàng)建一個(gè)調(diào)用客戶端:

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);
 }
}

運(yùn)行這個(gè)客戶端代碼,得到結(jié)果:

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>

RESTful的java客戶端調(diào)用案例源碼

使用Apache CXF開發(fā)RESTful服務(wù)

使用Apache CXF開發(fā)Web服務(wù)

使用Spring Webservices建立SOAP服務(wù)代理

REST專題

1

shefansxyh   2014-07-23    1討論    557瀏覽

如題,若應(yīng)用能夠支持一億 并發(fā) 量,應(yīng)用應(yīng)從哪幾個(gè)方面如手?請大伽不吝賜教!..

banq   2014-07-22    1討論    358瀏覽

Orleans 是微軟推出的類似Scala Akka的Actor模型,可用于實(shí)現(xiàn) DDD +EventSourcing/CQRS系統(tǒng)。 Orlea....

banq   2014-07-20    0討論    1059瀏覽

國內(nèi)大部分號稱 云計(jì)算 的產(chǎn)品基本是主機(jī)托管+數(shù)據(jù)中心,很多人認(rèn)為 ....

banq   2014-07-16    1討論    830瀏覽

這是一篇討論客戶端MVC和服務(wù)器端MVC的比較文章。 首先分離關(guān)注是架構(gòu)設(shè)計(jì)的一個(gè)基本原則,多層架構(gòu)中:數(shù)據(jù)存儲(chǔ) 服務(wù)層 API層和表現(xiàn)層各層之間應(yīng)該最小依賴,服務(wù)層只需要知道在哪里存儲(chǔ)數(shù)據(jù),A....

banq   2014-07-08    0討論    1226瀏覽

一位Node.js guru大牛 TJ Holowaychuk最近發(fā)表了 再會(huì)Node.js ,他曾經(jīng)是Empress, Mocha, Jade, Stylus Koa等參與者,TJ在這篇宣言中首先強(qiáng)....




    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多