SpringMVC REST请求风格

11 篇文章 0 订阅
1 篇文章 0 订阅

1、知识点

浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求,该过滤器为HiddenHttpMethodFilter。
HiddenHttpMethodFilter的父类是OncePerRequestFilter,它继承了父类的doFilterInternal方法,工作原理是将jsp页面的form表单的method属性值在doFilterInternal方法中转化为标准的Http方法,即GET,、POST、 HEAD、OPTIONS、PUT、DELETE、TRACE,然后到Controller中找到对应的方法。例如,在使用注解时我们可能会在Controller中用于@RequestMapping(value = “list”, method = RequestMethod.PUT),所以如果你的表单中使用的是,那么这个表单会被提交到标了Method=”PUT”的方法中。
REST:Representational State Transfer.即(资源)表现层状态转化。
资源(Resources):网络上的一个实体,它可以是一段文本、一张图片、一首歌曲、一种服务等。可以用一个URI指向它。第一种资源对应一个特定的URI.也就是说,URI即为每个资源的独一无二的识别符。
表现层(Representation):把资源具体呈现出来的形式,叫做它的表现层。比如,文本可以用txt格式表现,也可以用HTML格式、XML格式、JSON格式表现,甚至可以采用二进制格式。
状态转化(State Transfer):每发出一个请求,就代表了客户端和服务器一次交互过程。HTTP协议,是一个无状态协议,即所有的状态都保存在服务端。因此,如果客户想要操作服务器,必须通过某种手段,让服务端发生“状态转化”。而这种转化是建立在表现之上的。具体来说,就是HTTP协议里面,四个表示操作方式的动词
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/1 HTTP POST:新增id=1的order
HiddenHttpMethodFilter
浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持
Spring3.0添加一个过滤器,可以将这些请求转换为标准的http方法,使其支持GET、POST、PUT与DELETE请求
2、测试

在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>

控制器

@RequestMapping("/testREST")
@Controller
public class TestREST {
    @RequestMapping(value="/testRest/{id}",method=RequestMethod.GET)
    public String testRestGet(@PathVariable Integer id){
        System.out.println("testRest GET:"+id);
        return "success";
    }
    @RequestMapping(value="/testRest/{id}",method=RequestMethod.POST)
    public String testRestPost(@PathVariable Integer id){
        System.out.println("testRest POST:"+id);
        return "success";
    }
    @RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE)
    public String testRestDelete(@PathVariable Integer id){
        System.out.println("testRest Delete:"+id);
        return "success";
    }
    @RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT)
    public String testRestPut(@PathVariable Integer id){
        System.out.println("testRest Put:"+id);
        return "success";
    }

}

请求

<a href="testREST/testRest/1">测试Get</a>
     <form action="testREST/testRest/1" method="post">
        <input type="submit" value="测试post">
     </form> 
     <form action="testREST/testRest/1" method="post">
        <input type="hidden" name="_method" value="DELETE">
        <input type="submit" value="测试DELETE">
     </form> 
     <form action="testREST/testRest/1" method="post">
        <input type="hidden" name="_method" value="PUT">
        <input type="submit" value="测试PUT">
     </form> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值