Spring MVC 如何开发REST风格的应用呢?
REST(Representational State Transfer):表述性转移,是目前最流行的一种软件架构风格。它结构清晰、易于理解、有较好的扩展性。
Spring REST 风格:使用 URL 表示资源时,每个资源都可以使用一个独一无二的 URL 来表示,
并采用HTTP 方法进行操作,即 (GET、POST、PUT、DELETE),实现资源的增删改查。
GET: 获取资源
POST: 新建资源
PUT: 更新资源
DELETE: 删除资源
例:传统风格同REST风格URL对比
/cusview.html?id=88 VS /user/view/88
/cusdelete.html?id=88 VS /user/delete/88
/cuschange.html?id=88 VS /user/change/88
例:
在web.xml中配置过滤器 HiddenHttpMethodFilter
使其支持PUT和DELETE请求
如下所示:
新建一个REST请求页面
<%@ 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>REST风格</title>
</head>
<body>
<h4>发送GET请求</h4>
<a href=" cus/1">GET</a>
<h4>发送POST请求</h4>
<form action="cus/1" method="post">
<input type="submit" value="POST" />
</form>
<!-- 发送PUT和DELETE请求时,需要添加一个隐藏域 -->
<h4>发送PUT请求</h4>
<form action=" cus/1" method="post">
<input type="hidden" name="_method" value="PUT" /> <input
type="submit" value="PUT" />
</form>
<h4>发送DELETE请求</h4>
<input type="hidden" name="_method" value="DELETE" />
<form action=" cus/1" method="post">
<input type="hidden" name="_method" value="DELETE" /> <input
type="submit" value="DELETE" />
</form>
</body>
</html>
编写Controller
package com.java265.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;
public class CusController {
@RequestMapping(value = "/cus/{id}", method = RequestMethod.GET)
public String hello(@PathVariable Integer id) {
System.out.println("test rest get:" + id);
return "success";
}
@RequestMapping(value = "/cus/{id}", method = RequestMethod.POST)
public String hello() {
System.out.println("test POST:");
return "success";
}
@RequestMapping(value = "/cus/{id}", method = RequestMethod.DELETE)
public String helloDelete(@PathVariable Integer id) {
System.out.println("test rest delete:" + id);
return "success";
}
@RequestMapping(value = "/cus/{id}", method = RequestMethod.PUT)
public String helloPut(@PathVariable Integer id) {
System.out.println("test rest put:" + id);
return "success";
}
}
Spring REST 风格:使用 URL 表示资源时,每个资源都可以使用一个独一无二的 URL 来表示,
并采用HTTP 方法进行操作,即 (GET、POST、PUT、DELETE),实现资源的增删改查。
GET: 获取资源
POST: 新建资源
PUT: 更新资源
DELETE: 删除资源
例:传统风格同REST风格URL对比
/cusview.html?id=88 VS /user/view/88
/cusdelete.html?id=88 VS /user/delete/88
/cuschange.html?id=88 VS /user/change/88
例:
在web.xml中配置过滤器 HiddenHttpMethodFilter
使其支持PUT和DELETE请求
如下所示:
<!-- HiddenHttpMethodFilter过滤器可以将POST请求转化为put请求和delete请求! --> <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>
新建一个REST请求页面
<%@ 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>REST风格</title>
</head>
<body>
<h4>发送GET请求</h4>
<a href=" cus/1">GET</a>
<h4>发送POST请求</h4>
<form action="cus/1" method="post">
<input type="submit" value="POST" />
</form>
<!-- 发送PUT和DELETE请求时,需要添加一个隐藏域 -->
<h4>发送PUT请求</h4>
<form action=" cus/1" method="post">
<input type="hidden" name="_method" value="PUT" /> <input
type="submit" value="PUT" />
</form>
<h4>发送DELETE请求</h4>
<input type="hidden" name="_method" value="DELETE" />
<form action=" cus/1" method="post">
<input type="hidden" name="_method" value="DELETE" /> <input
type="submit" value="DELETE" />
</form>
</body>
</html>
编写Controller
package com.java265.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;
public class CusController {
@RequestMapping(value = "/cus/{id}", method = RequestMethod.GET)
public String hello(@PathVariable Integer id) {
System.out.println("test rest get:" + id);
return "success";
}
@RequestMapping(value = "/cus/{id}", method = RequestMethod.POST)
public String hello() {
System.out.println("test POST:");
return "success";
}
@RequestMapping(value = "/cus/{id}", method = RequestMethod.DELETE)
public String helloDelete(@PathVariable Integer id) {
System.out.println("test rest delete:" + id);
return "success";
}
@RequestMapping(value = "/cus/{id}", method = RequestMethod.PUT)
public String helloPut(@PathVariable Integer id) {
System.out.println("test rest put:" + id);
return "success";
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。