REST风格的网络接口

REST即(资源)表现层状态转化(英文:(Resource)Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。目前在三种主流的Web实现方案中,因为REST模式的Web服务与复杂的SOAP和XML-RPC对比来讲明显的更加简洁,越来越多的web服务开始采用REST风格设计和实现。例如,Amazon.com提供接近REST风格的Web服务进行图书查找;雅虎提供的Web服务也是REST风格的。

资源(Resource):任何事物,只要有被引用到的必要,它就是一个资源。资源可以是实体,也可以是一个抽象概念。可以用URI(统一资源定位符)指向它,每种资源对应一个特定的URI。要获取这个资源,访问它的URI即可,因此URI及为每一个资源的独一无二的识别符。

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

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

遵守REST体系结构约束的Web服务API称为RESTful API。基于HTTP的RESTful API定义有以下几个方面:

RESTful可以通过一套统一的接口为Web、iOS、Android提供服务。

以CRUD为例:

我们之前的方法请求可能是这样的:

news?id=1                    得到id=1的news

newsDelete?id=1          删除id=1的news

newsUpdate?id=1        修改id=1的news

newsAdd                        添加news

而REST风格的API增删改查都是一个地址,具体操作靠HTTP头部信息判断:

*/news/1    HTTP GET:得到id=1的news

*/news/1    HTTP DELETE:删除id=1的news

*/news/1    HTTP PUT:更新id=1的news

*/news    HTTP POST:新增news

浏览器form表单只支持GET和POST请求,Spring3.0之后添加了一个过滤器(HiddenHttpMethodFiler),可以将这些请求转化为标准的HTTP方法,使得支持GET、POST、PUT与DELETE请求

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	
	<!-- 配置org.springframework.web.filter.HiddenHttpMethodFilter:可以把POST请求转为DELETE或POST请求 -->
	<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>
	
	<!-- 配置前置控制器 -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- DispatcherServlet的一个初始化参数:配置SpringMVC 配置文件的位置和名称 -->
		<!-- 实际上也可以不通过contextConfigLocation来配置SpringMVC的配置文件,而使用默认的 默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml 
			springDispatcherServlet-servlet.xml -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

SpringMVCTest.java

package com.f145a.springmvc.handlers;

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;;

@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {
	
	private static final String SUCCESS="success";
	
	
	/**
	 * REST风格的URL
	 * 以CRUD为例:
	 * 新增:/order POST
	 * 修改:/order/1 PUT			update?id=1
	 * 获取:/order/1 GET			get?id=1
	 * 删除:/oeder/1 DELETE		delete?id=1
	 * 
	 * 如何发送PUT请求和DELETE请求呢?
	 * 1.需要配置HiddenHTTPMethodFilter
	 * 2.需要发送POST请求
	 * 3.需要在发送POST请求时携带一个name="_method"的隐藏域,值为DELETE或PUT
	 * 
	 * 在SpringMVC的目标方法中如何得到id呢?
	 * 使用@PathVariable注解
	 */
	@RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT)
	public String testRestPut(@PathVariable Integer id){
		System.out.println("testRest PUT:"+id);
		return SUCCESS;
	}
	
	@RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE)
	public String testRestDelete(@PathVariable Integer id){
		System.out.println("testRest Delete:"+id);
		return SUCCESS;
	}
	
	@RequestMapping(value="/testRest",method=RequestMethod.POST)
	public String testRest(){
		System.out.println("testRest POST");
		return SUCCESS;
	}
	
	@RequestMapping(value="/testRest/{id}",method=RequestMethod.GET)
	public String testRest(@PathVariable Integer id){
		System.out.println("testRest GET:"+id);
		return SUCCESS;
	}
}

index.jsp

<%@ 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>

<form action="springmvc/testRest/1" method="post">
	<input type="hidden" name="_method" value="PUT">
	<input type="submit" value="TestRest PUT">
</form>

<form action="springmvc/testRest/1" method="post">
	<input type="hidden" name="_method" value="DELETE">
	<input type="submit" value="TestRest DELETE">
</form>

<form action="springmvc/testRest" method="post">
	<input type="submit" value="TestRest POST">
</form>

<a href="springmvc/testRest/1">TestRest GET</a>

</body>
</html>

欢迎访问我的个人博客http://www.chengzequn.top

转载于:https://my.oschina.net/chengzequn/blog/780905

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值