Spring4MVC之REST

 

REST

REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用。

资源(Resources):网络上的一个实体,或者说是网络上的一个具体信息。它可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的存在。可以用一个URI(统一资源定位符)指向它,每种资源对应一个特定的 URI 。要获取这个资源,访问它的URI就可以,因此 URI 即为每一个资源的独一无二的识别符。

表现层(Representation):把资源具体呈现出来的形式,叫做它的表现层(Representation)。比如,文本可以用 txt 格式表现,也可以用 HTML 格式、XML 格式、JSON 格式表现,甚至可以采用二进制格式。

状态转化(State Transfer):每发出一个请求,就代表了客户端和服务器的一次交互过程。HTTP协议,是一个无状态协议,即所有的状态都保存在服务器端。因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生“状态转化”(State Transfer)。而这种转化是建立在表现层之上的,所以就是 “表现层状态转化”。

具体说,就是 HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源。

 

示例:

/order/1 HTTP GET :得到 id = 1 的 order

/order/1 HTTP DELETE:删除 id = 1的 order

/order/1 HTTP PUT:更新id = 1的 order

/order HTTP POST:新增 order

HiddenHttpMethodFilter:浏览器 form 表单只支持 GET 

与 POST 请求,而DELETE、PUT 等 method 并不支

持,Spring3.0 添加了一个过滤器,可以将这些请求转换

为标准的 http 方法,使得支持 GET、POST、PUT 与DELETE 请求。 

 

1. 需要配置 HiddenHttpMethodFilter 

  <!-- 
	配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 PUT 请求 
  -->
  <filter>
      <filter-name>hiddenHttpMethodFilter</filter-name>
      <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  
  <filter-mapping>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
  </filter-mapping>

2. 编辑JSP页面

2.1 需要发送 POST 请求

2.2 需要在发送 POST 请求时携带一个 name="_method" 的隐藏域, 值为 DELETE 或 PUT

 

<li>REST
            <ul>
                <li><a href="user/testRestMethod/10001">Get请求[10001]</a></li>
                <li>
                    <form action="user/testRestMethod" method="POST">
                        <input type="text" name="name"  value="POST请求"/>
                        <input type="submit"/>
                    </form>
                </li>
                <li>
                    <form action="user/testRestMethod/10001" method="POST">
                        <input type="hidden" name="_method" value="PUT">
                        <input type="text" name="name" value="PUT请求"/>
                        <input type="submit"/>
                    </form>
                </li>
                <li>
                    <form action="user/testRestMethod/10001" method="POST">
                        <input type="hidden" name="_method" value="DELETE">
                        <input type="text" name="name"  value="DELETE请求"/>
                        <input type="submit"/>
                    </form>
                </li>
            </ul>
        </li>

 

3. 编辑处理类

 

	/**
	 * Rest 风格的 URL. 以 CRUD 为例: 
	 * 新增: /order POST 
	 * 修改: /order/1 PUT update?id=1 
	 * 获取: /order/1 GET get?id=1 
	 * 删除: /order/1 DELETE delete?id=1
	 * 
	 * 如何发送 PUT 请求和 DELETE 请求呢 ? 
	 * 1. 需要配置 HiddenHttpMethodFilter 
	 * 2. 需要发送 POST 请求
	 * 3. 需要在发送 POST 请求时携带一个 name="_method" 的隐藏域, 值为 DELETE 或 PUT
	 * 
	 * 在 SpringMVC 的目标方法中如何得到 id 呢? 使用 @PathVariable 注解
	 * 
	 */
	@RequestMapping(value="/testRestMethod/{id}",method=RequestMethod.GET)
	public String testRestGetMethod(@PathVariable(value="id")Integer id)
	{
		System.out.println("------testRestGetMethod"+ id +"------");
		return LOGIN;
	}
	
	@RequestMapping(value="/testRestMethod",method=RequestMethod.POST)
	public String testRestPostMethod()
	{
		System.out.println("------testRestPostMethod------");
		return LOGIN;
	}
	
	@RequestMapping(value="/testRestMethod/{id}",method=RequestMethod.PUT)
	public String testRestPutMethod(@PathVariable(value="id")Integer id)
	{
		System.out.println("------testRestPutMethod"+ id +"------");
		return LOGIN;
	}
	
	@RequestMapping(value="/testRestMethod/{id}",method=RequestMethod.DELETE)
	public String testRestDeleteMethod(@PathVariable(value="id")Integer id)
	{
		System.out.println("------testRestDeleteMethod"+ id +"------");
		return LOGIN;
	}

 

 

 

猜你喜欢

转载自ihuning.iteye.com/blog/2242530
今日推荐