参考:http://my.oschina.net/huangyong/blog/294324 和http://www.ibm.com/developerworks/cn/opensource/os-restfulwebservices/index.html
今天我们将视角集中在 REST 上,它是继 SOAP 以后,另一种广泛使用的 Web 服务。与 SOAP 不同,REST 并没有 WSDL 的概念,也没有叫做“信封”的东西,因为 REST 主张用一种简单粗暴的方式来表达数据,传递的数据格式可以是 JSON 格式,也可以是 XML 格式,这完全由您来决定。
REST 全称是 Representational State Transfer(表述性状态转移),它是 Roy Fielding 博士在 2000 年写的一篇关于软件架构风格的论文,此文一出,震撼四方!许多知名互联网公司开始采用这种轻量级 Web 服务,大家习惯将其称为 RESTful Web Services
,或简称 REST 服务
。
那么 REST 到底是什么呢?
REST 本质上是使用 URL 来访问资源的一种方式。总所周知,URL 就是我们平常使用的请求地址了,其中包括两部分: 请求方式
与 请求路径
,比较常见的请求方式是 GET 与 POST,但在 REST 中又提出了其它几种其它类型的请求方式,汇总起来有六种:GET、POST、PUT、DELETE、HEAD、OPTIONS。尤其是前四种,正好与 CRUD(增删改查)四种操作相对应:GET(查)、POST(增)、PUT(改)、DELETE(删),这正是 REST 的奥妙所在!
实际上,REST 是一个“无状态”的架构模式,因为在任何时候都可以由客户端发出请求到服务端,最终返回自己想要的数据。也就是说,服务端将内部资源发布 REST 服务,客户端通过 URL 来访问这些资源,这不就是 SOA 所提倡的“面向服务”的思想吗?所以,REST 也被人们看做是一种轻量级的 SOA 实现技术,因此在企业级应用与互联网应用中都得到了广泛使用。
在 Java 的世界里,有一个名为 JAX-RS
的规范,它就是用来实现 REST 服务的,目前已经发展到了 2.0 版本,也就是 JSR-339 规范,如果您想深入研究 REST,请深入阅读此规范。
JAX-RS 规范目前有以下几种比较流行的实现技术:
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
《!--这两个是jax-rs的包--》
《!--这是cxf框架的包--》
《!--这是jaxb用的的包--因为jaxrs:server 配置里面用到"org.codehaus.jackson.jaxrs.JacksonJsonProvider" --》
在web.xml里面添加CXF支持:
在applicationContext.xml(位于resourse下面)添加cxf支持:
第二步:定义一个 REST 服务接口:
先定义实体类:
package com.jimmy.mywebservice.bean;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User {
private String userName;
private String emailId;
@XmlAttribute
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@XmlAttribute
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}
再定义接口服务类:
package com.jimmy.mywebservice.controller;
import java.io.InputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.jimmy.mywebservice.bean.User;
@Produces({ MediaType.APPLICATION_XML})
@Consumes({ MediaType.APPLICATION_XML})
public interface IRestStu {
@GET
@Path("/getUsers")
@Produces("application/xml")
public User getUsers();
@GET
@Path("/getUser/{id}")
@Produces("application/xml")
public User getUser(@PathParam("id") int id);
@POST
@Path("/createuser")
@Consumes("application/xml")
public Response createUser();
@PUT
@Path("/updateuser/{id}")
@Consumes("application/xml")
public Response updateUser(@PathParam("id") int id, InputStream is );
}
实现类:
public class IStuRestImpl implements IRestStu {
public User getUsers() {
User userDetails = new User();
userDetails.setUserName("Krishna");
userDetails.setEmailId("[email protected]");
System.out.println("test ");
return userDetails;
}
public Response getUserHtml() {
//Test HTML view
return Response.ok().build();
}
@Override
public User getUser(int id) {
User userDetails = new User();
userDetails.setUserName("Krishna");
userDetails.setEmailId("[email protected]");
System.out.println("test ");
return userDetails;
}
@Override
public Response createUser() {
// TODO Auto-generated method stub
return Response.ok().build();
}
@Override
public Response updateUser(int id, InputStream is) {
// TODO Auto-generated method stub
return Response.ok().build();
}
}
以上 StuService
接口中提供了一系列的方法,在每个方法上都使用了 JAX-RS 提供的注解,主要包括以下三类:
针对getUser方法,简单解释一下:
该方法将被 PUT://updateuser/{id}
请求来调用,请求路径中的 id
参数将映射到 long id
参数上,请求体中的数据将自动转换为 JSON 格式并映射到 Map
参数上,返回的 Product
类型的数据将自动转换为 JSON 格式并返回到客户端。
第三步:使用 CXF 发布 REST 服务
即配置rest-webservice.xml:
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
第四步:查看服务发布成功否:
http://localhost:8080/webservice/stuservices/v1.0?_wadl&_type=xml
//可见,尽管都是xml,但和soap的wsdl还是结构上很大不同