SpringMVC学习笔记3-----REST风格的四种请求方式

一、REST相关

REST:Representational State Transfer即表述层状态转化,是目前最流行的一种互联网软件架构,它结构清晰,符合标准,易于理解,扩展方便,得到很多网站的应用!

多的不说,展示的是操作,具体可去百度,或者直接:

https://blog.csdn.net/houjixin/article/details/54315835

https://blog.csdn.net/long0801/article/details/77679189

二、操作

在HTTP协议中,有四个常用的表示操作方式的动词:

GET  获取

POST  增加

PUT  修改

DELETE  删除

正好对应数据库中的增删改查操作!

以往添加参数我们常常在url后面带个问号然后再写参数名称=参数值

现在我们像下面一样表示:

/request/1    HTTP GET:得到id = 1的请求

/request/1    HTTP DELETE:闪出id=1的请求

/request/1    HTTP PUT:更新id = 1的请求

/request       HTTP POST:新增请求

可是我们平时只能发送get和post请求,怎么发送put和delete请求呢?

SpringMVC给我们提供了这样一个Filter:HiddenHttpMethodFilter可以将post请求转化为put和delete请求!其工作原理是在发送请求时拦截请求,获取一个名为“_method”代表的是请求方式的参数,如果检测到请求方式是post,然后再检测表单是否有代表()的隐藏域的参数,然后再将其转化为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>

然后 我们修改inde.jsp文件使其发出请求

    <!--REST发送get请求!-->
    <form action="/testREST/1" method="get">
        <input type="submit" value="测试 REST 的get请求!">
    </form>
    <br><br>
    <!--REST发送post请求!-->
    <form action="/testREST" method="post">
        <input type="submit" value="测试 REST 的post请求!">
    </form>
    <br><br>
    <!--REST发送delete请求!-->
    <form action="/testREST/1" method="post">
        <%--需要加上一个隐藏域--%>
        <input type="hidden" name="_method" value="DELETE">
        <input type="submit" value="测试 REST 的delete请求!">
    </form>
    <br><br>
    <!--REST发送put请求!-->
    <form action="/testREST/1" method="post">
        <%--需要加上一个隐藏域--%>
        <input type="hidden" name="_method" value="PUT">
        <input type="submit" value="测试 REST 的put请求!">
    </form>

然后在控制器中添加四个方法:

    //添加REST使用到的四个方法!
    @RequestMapping(value = "/testREST/{id}",method = RequestMethod.GET)
    public String testRestGet(@PathVariable Integer id) {
        System.out.println("testRestGet " + id);
        return "success";
    }

    @RequestMapping(value = "/testREST",method = RequestMethod.POST)
    public String testRestPost() {
        System.out.println("testRestPost ");
        return "success";
    }

    @RequestMapping(value = "/testREST/{id}",method = RequestMethod.DELETE)
    public String testRestDelete(@PathVariable Integer id) {
        System.out.println("testRestDelete " + id);
        return "success";
    }

    @RequestMapping(value = "/testREST/{id}",method = RequestMethod.PUT)
    public String testRestPut(@PathVariable Integer id) {
        System.out.println("testRestPut " + id);
        return "success";
    }

然后启动,点击post和get都能跳转,然而点击put和delete时就会出现错误!

百度得知这是因为发起的请求是个RESTFul风格的请求,调用了RESTFul风格的PUT方法。但是controller里testRestPUT返回的success字符串被映射到success.jsp。因此spring认为这应该是个JSP接口,且JSP接口仅仅支持GET方法和POST方法。所以系统提示提示了这个错误。

自己亲试有效的两种方法:

1.更换tomcat8到tomcat7

2.在success.jsp的页面头部加上

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
    多加一句话:isErrorPage设置为true,默认为false

启动服务器就都能使用了!

猜你喜欢

转载自blog.csdn.net/qq_38016931/article/details/81268123