一. Rest风格的API
所谓Rest风格的API,就是我们可以使用不同的请求方式来完成不同的CRUD操作
-
GET —> 获取数据 —> @GetMapping
-
PUT —> 修改数据 —> @PutMapping
-
POST —> 添加数据 —> @PostMapping
-
DELETE —> 删除数据 —> @DeleteMapping
接下来我们在Controller中,分别处理上面的四种请求
@Controller public class Test { @GetMapping(value = "/get", produces = "application/json;charset=utf-8") @ResponseBody public String get() { HashMap<Object, Object> map = new HashMap<>(); map.put("method", "get"); return JSON.toJSONString(map); } @PostMapping(value = "/post", produces = "application/json;charset=utf-8") @ResponseBody public String post() { HashMap<Object, Object> map = new HashMap<>(); map.put("method", "post"); return JSON.toJSONString(map); } @PutMapping(value = "/put", produces = "application/json;charset=utf-8") @ResponseBody public String put() { HashMap<Object, Object> map = new HashMap<>(); map.put("method", "put"); return JSON.toJSONString(map); } @DeleteMapping(value = "/delete", produces = "application/json;charset=utf-8") @ResponseBody public String delete() { HashMap<Object, Object> map = new HashMap<>(); map.put("method", "delete"); return JSON.toJSONString(map); } @RequestMapping("/test") public String test() { return "test"; } }
然后在test.jsp页面中分别对每个请求进行访问
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:set var="path" value="${pageContext.request.contextPath}"/> <!DOCTYPE html> <html> <head> <title>Title</title> </head> <body> <a href="${path}/get">Get</a> <hr /> <form action="${path}/get" method="get"> <input type="submit" value="Get"> </form> <form action="${path}/post" method="post"> <input type="submit" value="Post"> </form> <form action="${path}/put" method="put"> <input type="submit" value="put"> </form> <form action="${path}/delete" method="delete"> <input type="submit" value="delete"> </form> </body> </html>
当我们访问a标签、post、get时,浏览器能够正常发起请求,但是访问put和delete时,会显示405
这是因为浏览器只能够正常发起Get和Post两种请求,SpringMVC可以将post请求转换为put或者Delete
二. 将Post请求转换为Put和Delete
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>
2. 在请求参数中添加:name=”_method”(只能为该值),value为需要转换的你的请求方式
<form action="${path}/put" method="post"> <input type="hidden" name="_method" value="put"> <input type="submit" value="put"> </form> <form action="${path}/delete" method="post"> <input type="hidden" name="_method" value="delete"> <input type="submit" value="delete"> </form>
此时再次点击test.jsp中的put和Delete,浏览器已经可以正常发起put和delete
三. 使用路径传参
之前我们都是通过?参数名=参数值的方式传参,还可以通过路径传参,以京东的商品为例,每个商品页面都是一串数字+html结尾,该数字其实就是一个参数,也就是商品的唯一标识,.html其实是伪静态。
通过路径传参需要在Controller的方法的@GetMapping注解中添加{},{}中的内容即为参数名
@GetMapping("/productDetail/{id}") public String produtctDetail(@PathVariable Integer id){ System.out.println("id = " + id); return "test"; }
@PathVariable 将形参的id和@GetMapping(“/productDetail/{id}”)的id相关联
此时我们访问http://localhost:8888/sm/productDetail/123,则控制台输出id = 123,注意不是?id=123,而是直接/123,没有?也没有参数名
也可以在id后面添加html,设置伪静态:
@GetMapping("/productDetail/{id}.html") public String produtctDetail(@PathVariable Integer id){ System.out.println("id = " + id); return "test"; }
此时访问http://localhost:8888/sm/productDetail/123.html,控制台输出id = 123
请登录之后再进行评论