jersey创建restful服务及调用_用Jersey开发RESTful服务

用Jersey开发RESTful服务

REST基础概念:

在REST中的一切都被认为是一种资源。

每个资源由URI标识。

使用统一的接口。处理资源使用POST,GET,PUT,DELETE操作类似创建,读取,更新和删除(CRUD)操作。

无状态。每个请求是一个独立的请求。从客户端到服务器的每个请求都必须包含所有必要的信息,以便于理解。

通信都是通过展现。例如XML,JSON

RESTful Web服务由于其简单替代了基于SOAP的Web服务,并大型服务提供商所接受。这篇文章使用Jersey框架延伸JAX-RS API将展示如何创建一个REST风格的Web服务和客户端。

在Eclipse中,创建一个新的动态Web项目名为"RESTfulWS":

43754309_1.jpg

下载Jersey zip bundle 这里 here. 需要包:

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

43754309_2.jpg

加入项目:

43754309_3.jpg

创建Web服务类:

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 "" + "" + name + "" + "";

}

@GET

@Path("/age/{j}")

@Produces(MediaType.TEXT_XML)

public String userAge(@PathParam("j") int j) {

int age = j;

return "" + "" + age + "" + "";

}

}

配置web.xml

RESTfulWS

Jersey REST Service

com.sun.jersey.spi.container.servlet.ServletContainer

com.sun.jersey.config.property.packages

com.eviac.blog.restws

1

Jersey REST Service

/rest/*

点按这个项目 run as ->run on server.

浏览器浏览:http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra

URL说明:

43754309_4.jpg

创建一个调用客户端:

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 : Pavithra

*

* @param service

* @return

*/

private static String getResponse(WebResource resource) {

return resource.accept(MediaType.TEXT_XML).get(String.class);

}

}

运行这个客户端代码,得到结果:

Client Response

GET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra returned a response status of 200 OK

Response

Pavithra

Client Response

GET http://localhost:8080/RESTfulWS/rest/UserInfoService/age/25 returned a response status of 200 OK

Response

25

shefansxyh

2014-07-23

43754309_5.gif

1讨论

557浏览

如题,若应用能够支持一亿 并发 量,应用应从哪几个方面如手?请大伽不吝赐教!..

banq

2014-07-22

43754309_5.gif

1讨论

358浏览

Orleans 是微软推出的类似Scala Akka的Actor模型,可用于实现 DDD +EventSourcing/CQRS系统。 Orlea....

banq

2014-07-20

43754309_5.gif

0讨论

1059浏览

国内大部分号称 云计算 的产品基本是主机托管+数据中心,很多人认为 ....

banq

2014-07-16

43754309_5.gif

1讨论

830浏览

这是一篇讨论客户端MVC和服务器端MVC的比较文章。 首先分离关注是架构设计的一个基本原则,多层架构中:数据存储 服务层 API层和表现层各层之间应该最小依赖,服务层只需要知道在哪里存储数据,A....

banq

2014-07-08

43754309_5.gif

0讨论

1226浏览

一位Node.js guru大牛 TJ Holowaychuk最近发表了 再会Node.js ,他曾经是Empress, Mocha, Jade, Stylus Koa等参与者,TJ在这篇宣言中首先强....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值