一.restful风格介绍
比如说这个url http://blog.csdn.net/king866/article/details/52092095 就是restful风格的
REST是一种风格,并且形成了自己的规则,构建这样的应用,应尽量遵循REST的原则。
REST的主要原则有:
用URL表示资源。资源就像商业实体一样,是我们希望作为API实体呈现的一部分。通常是一个名词,每个资源都用一个独一无二的URL来表示。
HTTP方法表示操作。REST充分利用了HTTP的方法,特别是GET、POST、PUT和DELETE。注意XMLHttpRequest对象实现了全部的方法,具体可以参看W3C HTTP 1.1 Specification。
也就是说,客户端的任何请求都包含一个URL和一个HTTP方法。回到上面的例子中,比赛显然是一个实体,那么对于一个特定比赛的请求就表示为:
这种方式是清晰明了的,也许和精确命名的方式有所区别,但是只要遵循这种形式,我们就能很快的进行GET、DELETE、UPDATE和新建操作。
Spring对RESTful的支持
- Spring MVC 对 RESTful应用提供了以下支持
- 利用@RequestMapping 指定要处理请求的URI模板和HTTP请求的动作类型
- 利用@PathVariable讲URI请求模板中的变量映射到处理方法参数上
- 利用Ajax,在客户端发出PUT、DELETE动作的请求
RequestMapping的一般应用格式。
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) // 因为这个需要Ajax请求,所有返回的是个json
@ResponseBody
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
资源(Resources)
REST的名称"表现层状态转化"中,省略了主语。"表现层"其实指的是"资源"(Resources)的"表现层"。
所谓"资源",就是网络上的一个实体,或者说是网络上的一个具体信息。它可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的实在。你可以用一个URI(统一资源定位符)指向它,每种资源对应一个特定的URI。要获取这个资源,访问它的URI就可以,因此URI就成了每一个资源的地址或独一无二的识别符。
二.JAX-RS 简介
JAX-RS是一个基于annotation的API,用来实现RESTful web services,使用Java,基于HTTP。本质上,类和函数通过annotation信息作为一个资源暴露给运行时——而这种方式通过servlet编程模式很难实现。一个运行时能够处理HTTP协议和Java类之间的JAX-RS信息,使用URIs,请求和接受内容类型,和HTTP函数。Sun附加提供的参考实现:Jersey,其他实现也可以使用了(不同的平台有不同竞争者):比如 Restlet 框架,JBoss RESTeasy project,和 Apache CXF web services 解决方案。
三.JAX-RS 注解
@Path
用来映射 URI,为资源类以及资源类中包含的方法提供访问路径。
@GET
表示处理 HTTP GET 请求的资源类方法。当 Web Service 获得客户端发出的对与某个网络资源 的 HTTP GET 操作时,服务器会调用被 @GET 注解后的方法来处理 GET 请求。当然,被调用的资源类方 法首先得满足 URI。
@POST
表示处理 HTTP POST 请求的资源类方法。和 @GET 相类似,只不过对应的是 HTTP POST 操作 。
@PUT
表示处理 HTTP PUT 请求的资源类方法。该 Annotation 通常用于更新网络对象的方法。和 @GET,@POST 处理流程相类似。
@DELETE
表示处理 HTTP DELETE 请求的资源类方法。使用该 Annotation 后的方法通常是删去每个 网络对象的实例。处理流程和 @GET,@POST,@PUT 相类似。
@HEAD
表示处理 HTTP HEAD 请求的资源类方法。通常情况下,根据 JAX-RS 规范的设定,在没有实 现 @HEAD 的资源类方法时,RESTlet JAX-RS extension 会自动处理 HTTP HEAD 请求,@GET 注解的资源 类方法会自动被调用。和处理普通的 HTTP GET 请求的区别是没有实例被返回。@HEAD 注解的资源类方法 通常用来获取 Web Services 能够接受的数据格式。
@Produces
用来表示资源类方法能够返回的 MIME 的媒体类型。
@Consumes
用来表示资源类方法能够处理的 MIME 的媒体类型。
四.用cxf搭建restful风格的webservice
1.添加maven依赖
2.新建User.java类,代码如下:
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public class User {
private static final long serialVersionUID = 677484458789332877L;
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.新建UserService.java类,代码如下:
@Path(value = "/users")
public interface UserService {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String doGet();
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("request/{param}")
public String doRequest(@PathParam("param") String param,
@Context HttpServletRequest servletRequest,
@Context HttpServletResponse servletResponse);
/**
* 通过用户id查询用户信息
*
* @param userId
* 用户id
* @return 用户信息
*/
@GET
@Path("/user/{userId}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public User getUser(@PathParam("userId") int userId);
/**
* 添加用户信息
*
* @param user
* 用户信息
* @return 返回用户信息
*/
@POST
@Path("/save")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public User saveUser(User user);
/**
* 更新用户信息
*
* @param userId
* 用户id
* @return 返回用户信息
*/
@PUT
@PathParam("/user/{userId}")
@Produces({ MediaType.APPLICATION_XML })
public User updateUser(@PathParam("userId") int userId);
/**
* 删除用户信息
*
* @param userId
* 根据用户id删除用户信息
*/
@DELETE
@Path("/user/{userId}")
public void deleteUser(@PathParam("userId") int userId);
4.新建UserServiceImpl.java类,代码如下:
public class UserServiceImpl implements UserService {
@Override
public String doGet() {
return "this is get rest request";
}
@Override
public String doRequest(String param, HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {
System.out.println("##########doRequest()#########");
return "success";
}
@Override
public User getUser(int userId) {
System.err.println("####获取用户信息成功#####");
User user = new User();
user.setId(userId);
user.setName("test");
return user;
}
@Override
public User saveUser(User user) {
System.out.println("##########添加用户成功!###########");
return user;
}
@Override
public User updateUser(int userId) {
System.out.println("##########更新用户成功!###########");
User user = new User();
user.setId(userId);
user.setName("test");
return user;
}
@Override
public void deleteUser(int userId) {
System.out.println("##########删除用户成功!###########");
}
}
五.新建applicationContext-cxf-jax-restful.xml配置文件配置,配置如下:
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
六.web.xml配置文件添加cxf配置信息,代码如下:
七.访问链接http://localhost:8083/cxf_server/webservice/restful/users/user/1,出现如下图界面,说明发布成功。