博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Java创建RESTful Web Service
阅读量:4152 次
发布时间:2019-05-25

本文共 4000 字,大约阅读时间需要 13 分钟。

REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移)。2000年Roy Fielding博士在他的博士论文《体系结构与基于网络的软件架构设计》中提出了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接口。本文示例代码使用和Java SE 6编写。

 

创建RESTful Web Service服务端

 

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

 

 

 

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定义了类的层次路径。// 指定了资源类提供服务的URI路径。@Path("UserInfoService")public class UserInfo { // @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") int j) { 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

输出结果如下:

创建客户端

 

 

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

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));} /*** 返回客户端请求。* 例如:* GET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra* 返回请求结果状态“200 OK”。** @param service* @return*/private static String getClientResponse(WebResource resource) {return resource.accept(MediaType.TEXT_XML).get(ClientResponse.class).toString();} /*** 返回请求结果XML* 例如:
Pavithra
** @param service* @return*/private static String getResponse(WebResource resource) {return resource.accept(MediaType.TEXT_XML).get(String.class);}}

 运行客户端程序后,可以看到以下输出:

Client ResponseGET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra returned a response status of 200 OKResponse
Pavithra
Client ResponseGET http://localhost:8080/RESTfulWS/rest/UserInfoService/age/25 returned a response status of 200 OKResponse
25

 

 

转载地址:http://zplti.baihongyu.com/

你可能感兴趣的文章
螺旋矩阵
查看>>
旋转有序数组的二分查找
查看>>
vector.resize
查看>>
python
查看>>
cout的输出顺序
查看>>
楼层扔鸡蛋
查看>>
linux删除空行 基本操作
查看>>
各种正则
查看>>
正则表达式shell
查看>>
python作用域
查看>>
重载赋值运算符
查看>>
返回值优化
查看>>
vim复制
查看>>
输出所有的合法的括号组合
查看>>
子集和问题
查看>>
vim 基本设置
查看>>
perl 参数传递
查看>>
Zeromq
查看>>
程序员40岁之后怎么办
查看>>
isnan isinf
查看>>