rest是一个架构风格,用url来访问网络上的任何资源。rest的一种思想就是用http中的动作get,post,put,delete,来进行增删改查。
这里介绍的是springMVC的rest请求。 不包含webservice的JAX-RS的例子。rest风格的webservice可以用cxf框架进行实现。也很简单。
1 首先准备web项目需要的jar包,也就是springMVC所依赖的jar:
2 创建一个动态的web工程:这里首先需要配置web.xml文件注册springMVC的前端控制器,dispatcherServlet,所有的客户端请求会被他进行转发。然后在配置hiddenHttpMethodFilter用来把post请求转成put 和 delete
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- hiddenHttpMethodFilter 可以把把post请求转船成put delete --></br></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">filter</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">filter-name</span><span style="color: #0000ff;">></span>hiddenHttpMethodFilter<span style="color: #0000ff;"></</span><span style="color: #800000;">filter-name</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">filter-class</span><span style="color: #0000ff;">></span>org.springframework.web.filter.HiddenHttpMethodFilter<span style="color: #0000ff;"></</span><span style="color: #800000;">filter-class</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"></</span><span style="color: #800000;">filter</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">filter-mapping</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">filter-name</span><span style="color: #0000ff;">></span>hiddenHttpMethodFilter<span style="color: #0000ff;"></</span><span style="color: #800000;">filter-name</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">url-pattern</span><span style="color: #0000ff;">></span>/*<span style="color: #0000ff;"></</span><span style="color: #800000;">url-pattern</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"></</span><span style="color: #800000;">filter-mapping</span><span style="color: #0000ff;">></span></br></br>
<span style="color: #008000;"><!--</span><span style="color: #008000;"> 配置springmvc的dispatcherServlet分发请求,实际上他是一个前端控制器 </span><span style="color: #008000;">--></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">servlet</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">></span>dispatcherServlet<span style="color: #0000ff;"></</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">servlet-class</span><span style="color: #0000ff;">></span>org.springframework.web.servlet.DispatcherServlet<span style="color: #0000ff;"></</span><span style="color: #800000;">servlet-class</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"></</span><span style="color: #800000;">servlet</span><span style="color: #0000ff;">></span></br></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">servlet-mapping</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">></span>dispatcherServlet<span style="color: #0000ff;"></</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">url-pattern</span><span style="color: #0000ff;">></span>/<span style="color: #0000ff;"></</span><span style="color: #800000;">url-pattern</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"></</span><span style="color: #800000;">servlet-mapping</span><span style="color: #0000ff;">></span></br>
</web-app>
3 编写rest风格的接口
package cn.bean.demo.service;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value="/restservice")
public class RestService {
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">final</span> <span style="color: #0000ff;">static</span> String SUCCEEDD="show"<span style="color: #000000;">;</br></br>
</span><span style="color: #008000;">/**</span><span style="color: #008000;"></br>
* get请求</br>
* url: </span><span style="color: #008000; text-decoration: underline;">http://localhost</span><span style="color: #008000;">:8080/springmvc/restservice/testRestGet/12</br>
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> id </br>
* 查询的参数</br>
* </span><span style="color: #808080;">@return</span></br>
<span style="color: #008000;">*/</span><span style="color: #000000;"></br>
@RequestMapping(value</span>="/testRestGet/{id}",method=<span style="color: #000000;">RequestMethod.GET)</br>
</span><span style="color: #0000ff;">public</span> String testRestGet(@PathVariable("id"<span style="color: #000000;">) Integer id){</br>
System.out.println(</span>"rest 风格的GET请求..........id=" +<span style="color: #000000;">id);</br>
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> SUCCEEDD;</br>
}</br>
</span><span style="color: #008000;">/**</span><span style="color: #008000;"></br>
* post新增 </br>
* url: </span><span style="color: #008000; text-decoration: underline;">http://localhost</span><span style="color: #008000;">:8080/springmvc/restservice/testRestPost
* </span><span style="color: #808080;">@return</span></br>
<span style="color: #008000;">*/</span><span style="color: #000000;"></br>
@RequestMapping(value</span>="/testRestPost",method=<span style="color: #000000;">RequestMethod.POST)</br>
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String testRestPost(){</br>
System.out.println(</span>"rest 风格的POST请求.......... "<span style="color: #000000;">);</br>
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> SUCCEEDD;</br>
}</br>
</span><span style="color: #008000;">/**</span><span style="color: #008000;"></br>
* PUT 修改操作</br>
* url: </span><span style="color: #008000; text-decoration: underline;">http://localhost</span><span style="color: #008000;">:8080/springmvc/restservice/testRestPut/put123</br>
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> name</br>
* </span><span style="color: #808080;">@return</span></br>
<span style="color: #008000;">*/</span><span style="color: #000000;"></br>
@RequestMapping(value</span>="/testRestPut/{name}",method=<span style="color: #000000;">RequestMethod.PUT)</br>
</span><span style="color: #0000ff;">public</span> String testRestPut(@PathVariable("name"<span style="color: #000000;">) String name){</br>
System.out.println(</span>"rest 风格的PUT请求..........name="+<span style="color: #000000;">name);</br>
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> SUCCEEDD;</br>
}</br>
</span><span style="color: #008000;">/**</span><span style="color: #008000;"></br>
* DELETE删除操作</br>
* url: </span><span style="color: #008000; text-decoration: underline;">http://localhost</span><span style="color: #008000;">:8080/springmvc/restservice/testRestDelete/11</br>
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> id</br>
* </span><span style="color: #808080;">@return</span></br>
<span style="color: #008000;">*/</span><span style="color: #000000;"></br>
@RequestMapping(value</span>="/testRestDelete/{id}",method=<span style="color: #000000;">RequestMethod.DELETE)</br>
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String testRestDelete(@PathVariable Integer id){</br>
System.out.println(</span>"rest 风格的DELETE请求..........id="+<span style="color: #000000;">id);</br>
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> SUCCEEDD;</br>
}</br></br>
}
4 编写接口的响应页面 -对应着接口的return
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>show this is succeedd ? yes </h2>
</body>
</html>
5 发布和配置rest接口 dispatcherServlet-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<span style="color: #008000;"><!--</span><span style="color: #008000;"> 配置自定扫描的包 </span><span style="color: #008000;">--></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">context:component-scan </span><span style="color: #ff0000;">base-package</span><span style="color: #0000ff;">="cn.bean.demo"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">context:component-scan</span><span style="color: #0000ff;">></span></br></br>
<span style="color: #008000;"><!--</span><span style="color: #008000;"> 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 </span><span style="color: #008000;">--></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="org.springframework.web.servlet.view.InternalResourceViewResolver"</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="prefix"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="/WEB-INF/views/"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">property</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"><</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="suffix"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">=".jsp"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">property</span><span style="color: #0000ff;">></span></br>
<span style="color: #0000ff;"></</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">></span></br></br>
</beans>
6 测试rest接口: 将项目发布到tomcat7中。 测试工具RESTClient