java restful service_使用Java创建RESTful Web Service

REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移)。2000年Roy Fielding博士在他的博士论文“Architectural Styles and the Design of Network-based Software Architectures”《体系结构与基于网络的软件架构设计》中提出了REST。

REST是一种体系结构。而HTTP是一种包含了REST架构属性的协议。

REST基础概念

在REST中所有东西都被看作资源。每一个资源都有一个URI和它对应。

在REST中使用统一接口处理资源。与数据库CRUD操作(Create、Read、Update 和 Delete)一样,可以用POST、GET、PUT和DELETE处理REST资源。

每个REST请求都是孤立的,请求中包含了所需的全部信息。REST服务端不存储状态。

REST支持不同的通信数据格式,比如XML、JSON。

RESTful Web Services

RESTful Web Services因其简单性被广泛使用,它比SOAP要更简单。本文将重点介绍如何使用Jersey框架创建RESTful Web Services。Jersey框架实现了JAX-RS接口。本文示例代码使用

创建RESTful Web Service服务端

在Eclipse中创建一个“dynamic web project”(动态web工程) ,项目名设为 “RESTfulWS”。

new-dynamic-project1-258x300.png

从这里下载Jersey。示例代码使用的是Jersey 1.17.1。首先解压Jersey到“jersey-archive-1.17.1”文件夹。接着将里面lib文件夹下的jar文件拷贝到工程目录的WEB-INF -> lib。然后将它们添加到build path。

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”包,并在其中创建“UserInfo”类。最后把web.xml拷贝到WEB-INF目录下。

add-jars.png

setting-jar-path.png

UserInfo.java

packagecom.eviac.blog.restws;importjavax.ws.rs.GET;importjavax.ws.rs.Path;importjavax.ws.rs.PathParam;importjavax.ws.rs.Produces;importjavax.ws.rs.core.MediaType;/***

*@authorpavithra

**///这里@Path定义了类的层次路径。//指定了资源类提供服务的URI路径。

@Path("UserInfoService")public classUserInfo {//@GET表示方法会处理HTTP GET请求

@GET//这里@Path定义了类的层次路径。指定了资源类提供服务的URI路径。

@Path("/name/{i}")//@Produces定义了资源类方法会生成的媒体类型。

@Produces(MediaType.TEXT_XML)//@PathParam向@Path定义的表达式注入URI参数值。

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") intj) {int age =j;return "" + "" + age + "" + "";

}

}

web.xml

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5">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/*

将此URL拷贝到浏览器地址栏中运行:

http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra

web-service-url.png

输出结果如下:

browser-result.png

创建客户端

创建一个“com.eviac.blog.restclient”包,然后新建“UserInfoClient”类。

UserInfoClient.java

packagecom.eviac.blog.restclient;importjavax.ws.rs.core.MediaType;importcom.sun.jersey.api.client.Client;importcom.sun.jersey.api.client.ClientResponse;importcom.sun.jersey.api.client.WebResource;importcom.sun.jersey.api.client.config.ClientConfig;importcom.sun.jersey.api.client.config.DefaultClientConfig;/***

*@authorpavithra

**/

public classUserInfoClient {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 voidmain(String[] args) {

String name= "Pavithra";int age = 25;

ClientConfig config= newDefaultClientConfig();

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

}/*** 返回客户端请求。

* 例如:

* GEThttp://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra

* 返回请求结果状态“200 OK”。

*

*@paramservice

*@return

*/

private staticString getClientResponse(WebResource resource) {return resource.accept(MediaType.TEXT_XML).get(ClientResponse.class)

.toString();

}/*** 返回请求结果XML

* 例如:Pavithra

*

*@paramservice

*@return

*/

private staticString 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

ResponsePavithraClient Response

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

Response25

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值