RESTFul该如何使用?
RESTFul简介
REST:Representational State Transfer,表现层资源状态转移。
RESTFul实现
具体说,就是 HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源。 REST 风格提倡 URL 地址使用统一的风格设计,从前到后各个单词使用斜杠分开,不使用问号键值对方 式携带请求参数,而是将要发送给服务器的数据作为 URL 地址的一部分,以保证整体风格的一致性。
-
get、post请求实现
那User来举例,一个增删改查有5套流程
- 查询所有用户信息 GET
- 根据id查询指定用户信息 GET
- 增加用户信息 POST
- 删除用户信息 DELETE
- 修改用户信息 PUT
我们知道超链接发送的是get请求,表单既可以发送get也可以发送post
<a th:href="@{/user}">查询所有用户信息</a><br> <a th:href="@{/user/1}">根据id查询用户信息</a><br> <form th:action="@{/user}" method="post"> 用户名:<input type="text" name="username"> <br> 密码:<input type="password" name="password"><br> <input type="submit" value="添加"> </form> <form th:action="@{/user}" method="post"> <input type="hidden" name="_method" value="put"> 用户名:<input type="text" name="username"> <br> 密码:<input type="password" name="password"><br> <input type="submit" value="修改"> </form>
@RequestMapping(value = "/user",method = RequestMethod.GET) public String queryAllUser(){ System.out.println("查询所有用户信息"); return "success"; } @RequestMapping(value = "/user/{id}" , method = RequestMethod.GET) public String queryUserById(){ System.out.println("根据指定id查询用户信息"); return "success"; } @RequestMapping(value = "/user" , method = RequestMethod.POST) public String addUser(String username,String password){ System.out.println("添加用户信息:" + username + "," + password); return "success"; }
-
put、delete请求实现
之前测试过,在表单中设置put或delete请求时,系统因为没有这种类型的方法,会默认使用get请求来发送数据,这里要使用HiddenHttpMethodFilter过滤器就可以解决此问题
看一下HiddenHttpMethodFilter过滤器的源码,主要关注doFilterInternal方法
public class HiddenHttpMethodFilter extends OncePerRequestFilter { private static final List<String> ALLOWED_METHODS; public static final String DEFAULT_METHOD_PARAM = "_method"; private String methodParam = "_method"; public HiddenHttpMethodFilter() { } public void setMethodParam(String methodParam) { Assert.hasText(methodParam, "'methodParam' must not be empty"); this.methodParam = methodParam; } protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { //复制一个请求 HttpServletRequest requestToUse = request; //有第一条判断信息,请求必须为post请求才能通过,第二个条件恒为true if ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) { //获取传输过来的method方法 String paramValue = request.getParameter(this.methodParam); if (StringUtils.hasLength(paramValue)) { //将值转化为大写 String method = paramValue.toUpperCase(Locale.ENGLISH); //判断方法名是否包含在ALLOWED_METHODS中 if (ALLOWED_METHODS.contains(method)) { //调用HttpMethodRequestWrapper将传过来的method值注入到新的请求中 requestToUse = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method); } } } //新的request请求代替原本的request请求 filterChain.doFilter((ServletRequest)requestToUse, response); } static { //ALLOWED_METHODS是一个集合,里面包含PUT、DELETE、PATCH ALLOWED_METHODS = Collections.unmodifiableList(Arrays.asList(HttpMethod.PUT.name(), HttpMethod.DELETE.name(), HttpMethod.PATCH.name())); } private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper { private final String method; public HttpMethodRequestWrapper(HttpServletRequest request, String method) { super(request); this.method = method; } public String getMethod() { return this.method; } } }
以上我们可以得到两个结论,第一必须要满足请求为POST请求,第二必须要传入一个_method值为put或delete
看一下如何实现
首先要在web.xml文件中设置请求方法的过滤器,需要注意的是,该过滤器会获取请求参数,而多个过滤器一起设置时,写在前面的先执行,所以此过滤器要设置在字符集过滤器后面
<!--设置请求方法过滤器--> <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>
@RequestMapping(value = "/user" , method = RequestMethod.PUT) public String updateUser(){ System.out.println("修改用户信息"); return "success"; }
<form th:action="@{/user}" method="post"> <input type="hidden" name="_method" value="put"> 用户名:<input type="text" name="username"> <br> 密码:<input type="password" name="password"><br> <input type="submit" value="修改"> </form>
这里那put来举例,delete和put操作方法完全一致,不同的是将隐藏域中的put改成delete。另外像删除这种功能我们一般都写成超链接,可以设置js令超链接跳转失效,写一个form让点击超链接时触发即可。