REST 概述
REST (Representational State Transfer)即表述性状态传递。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。在三种主流的 web 服务实现方案中,因为 REST 模式的 web 服务于复杂的 SOAP 和 XML-RPC 对比来讲明显的更加简洁。
资源(Resources):网络的一个实体,或者说是网络上的一个具体信息。它可以是一段文本,一张图片,一首歌曲,一种服务,总之就是一个具体的存在。可以用一个 URI 执行它,每种资源对应一个特定的 URI。
表现层(Representation):把资源具体呈现出来的形式,叫做它的表现层。比如,文本可以用 txt格式表现,也可以用 HTML, XML, JSON 格式表现。
状态转化(State Transfer):每发出一个请求,就代表客户端和服务器端的一次交互过程。HTTP 协议是一个无状态协议,即所有的状态都保存在服务器端。因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生“状态转化”。而这种转化是建立在表现层之上的。具体说,就是 HTTP 协议里面,四个表示操作方法的词:GET, POST, PUT, DELETE。他们分别对应四种基本操作:GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源。
例:
/order/1 HTTP GET // 得到 id=1 的 order
/order/1 HTTP DELETE // 删除 id=1 的 order
/order/1 HTTP PUT // 更新 id=1 的 order
/order HTTP POST // 新增 order
SpringMVC 搭建 REST 请求
1. 配置 HiddenHttpMethodFilter 过滤器
浏览器 form 表单中只支持 GET 与 POST 请求,而 DELETE, PUT 等 method 并不支持。Spring3.0 添加了一个过滤器,可以将这些请求转换为标准的 HTTP 方法,使得支持 GET, POST, PUT, DELETE 请求。
在 web.xml 中创建 HiddenHttpMethodFilter 过滤器
<?xml version="1.0" encoding="UTF-8"?> dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml 1 dispatcherServlet / characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 forceRequestEncoding true forceResponseEncoding true characterEncodingFilter /* HiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter HiddenHttpMethodFilter /*
2. 创建表单
创建表单,提交方式为 POST,并创建一个携带 name="_method" 的隐藏域,值为 DELETE 或 PUT。
Title
TestRest GET
3. 接收请求
利用 @RequestMapping 注解及其里面的属性 method 来接收 REST 的请求并定位 请求的方式。
利用 @PathVariable 注解接收参数
package com.manman.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;import org.springframework.web.bind.annotation.ResponseBody;@Controller@RequestMapping("/springmvc")public class MyController { /** * GET 请求,根据 id 得到信息 * @param id * @return */ @RequestMapping(value="/testRest/{id}", method = RequestMethod.GET) public String testRest(@PathVariable Integer id){ // 根据 id 在数据库中得到数据 System.out.println("testRest GET:" + id); return "success"; } /** * POST 请求,新增数据 * @return */ @RequestMapping(value="/testRest", method = RequestMethod.POST) public String testRest(){ // 在数据库中新增数据 System.out.println("testRest POST"); return "success"; } /** * DELETE 根据 id 删除数据 * @param id * @return */ @RequestMapping(value="/testRest/{id}", method = RequestMethod.DELETE) public String testRestDelete(@PathVariable Integer id){ // 根据 id 删除数据库里对应的数据 System.out.println("testRest DELETE:" + id); return "success"; } /** * PUT 根据 id 更新数据 * @param id * @return */ @RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT) public String testRestPut(@PathVariable Integer id){ // 根据 id 更新数据库里对应的数据 System.out.println("testRest PUT:" + id); return "success"; }}
出现的问题: HTTP 状态 405 - 方法不允许,jsp 只允许 GET, POST 或 HEAD。
![a47a54e71b4ce2fb43cbb0e9979f7f26.png](https://i-blog.csdnimg.cn/blog_migrate/22d54a2f4ed49853465d189bdb5ff036.jpeg)
由于 JSP 2.3(Tomcat 8)仅支持 JSP 的方法是 GET POST 或 HEAD。这是一个很大的不兼容的变化。
解决方案一
返回的页面是 405,说明网络请求没有问题,并且根据控制器有输出数据,故经过了方法。故可以将 tomcat 降到 7.0 及以下。
劣势:需要改变整个项目的版本。及不推荐
解决方案二
利用重定向方法 将 PUT 和 DELETE 请求的资源重新跳转到新的页面。
@RequestMapping(value = "/book/{id}",method = RequestMethod.PUT)public String put(@PathVariable("id") Integer id) { System.out.println("更新"+id+"号图书"); return "redirect:/hello/toput";}
劣势:由于重定向不能得到前面请求的 request 域中的数据,且重定向数据无法访问到 WEB-INF 下的安全页面。故不太推荐。
解决方案三
使用 @Respongsebody 注解
@ResponseBody@RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)public String testRestPut(@PathVariable Integer id){ System.out.println("testRest PUT:" + id); return "success";}
可执行的原因:
@ResponseBody 的作用虽然是将 java 地下转换为 json 格式的数据。
但在默认的 Spring 配置中,当返回类型为 String 的 Controller 方法时,在其上加上 @ResponseBody 时返回的就不再时我们期待的 json 格式的字符串,而是会变成正常的 json 串前加上双引号。故此返回的也是字符串,会被视图解析器解析执行。