REST 风格的请求方式

REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便, 因此正获得愈来愈多网站的采用。使用 REST 风格的请求方式,能够简化 url,达到使用同一个 url 不一样请求方式来执行不一样的方法。html

REST 风格的请求方式分别对应了如下四种请求,这四种请求有分别对应了四种对资源的操做:java

GET -----------> 获取资源web

POST ---------> 新建资源spring

PUT -----------> 更新资源架构

DELETE ------> 删除资源app

GET

GET 请求咱们都很熟悉了,好比地址栏直接访问,超连接访问等。jsp

咱们建立一个控制器用来接收 GET 请求:post

package com.pudding.controller;

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
public class RestController {

	@RequestMapping(value = "/rest/{id}", method = RequestMethod.GET)
	public String get(@PathVariable Integer id) {
		System.out.println("GET --- 查询数据 --- " + id);
		return "success";
	}

}

而且使用一个超连接来发出 GET 请求:网站

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Rest风格请求</title>
</head>
<body>
	<a href="rest/1">GET 请求</a>
</body>
</html>

访问页面,点击页面上的超连接,会发现画面跳转到了 success.jsp 而且在控制台上输出了 "GET --- 查询数据 --- 1" 。说明控制器成功的接收到了 GET 请求而且获取到了 url 地址中的参数。url

POST

POST 请求最多见的方式就是 form 表单了。

咱们建立一个控制器来接收 POST 请求:

package com.pudding.controller;

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
public class RestController {

	@RequestMapping(value = "/rest", method = RequestMethod.POST)
	public String post() {
		// 接收表单中的各类信息
		System.out.println("POST --- 建立数据");
		return "success";
	}

}

而且使用一个 form 表单来发出 POST 请求:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="rest" method="post">
		<input type="submit" value="POST 请求" />
	</form>
</body>
</html>

访问页面,点击页面上表单中的按钮,会发现画面跳转到了 success.jsp 而且在控制台上输出了 "POST --- 建立数据" 。说明控制器成功的接收到了 GET 请求。

PUT 和 DELETE

建立 GET 请求和建立 PUT 请求都很简单,可是 PUT 请求和 DELETE 请求呢?在咱们正常的访问当中是建立不出 PUT 请求和 DELETE 请求的,因此咱们须要使用 Spring 三大组件之一的过滤器Filter来发出 PUT 请求和 DELTE 请求。

发送 PUT 和 DELTE 请求的步骤:

  1. 在 web.xml 中添加HiddenHttpMethodFilter过滤器:
<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>
  1. 建立一个带 POST 请求的 form 表单:
<form action="rest" method="post">
		<input type="submit" value="PUT 请求" />
	</form>
  1. 在表单内添加 name 为 _method 的标签:
<form action="rest" method="post">
		<input type="hidden" name="_method" value="put" />
		<input type="submit" value="PUT 请求" />
	</form>

DELTE 请求同理,只须要将 put 求改成 delte 便可。不区分大小写。

若是按照以上步骤处理完成以后,点击按钮发现 HTTP 405 的错误提示:"消息 JSP 只容许 GET、POST 或 HEAD。Jasper 还容许 OPTIONS"。那么请参 HTTP 405 的错误提示:消息 JSP 只容许 GET、POST 或 HEAD。Jasper 还容许 OPTIONS 的解决方法 章节。

相关文章
相关标签/搜索