RESTful配置以及测试

RESTful介绍

REST:即Representational State Transfer ,(资源)表现层状态转化,是目前最流行的一种互联网软件架构。
具体说,就是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

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>

测试

需要注意: 由于doFilterInternal方法只对method为post的表单进行过滤,所以在页面中必须如下设置:

  <form action="..." method="post">  
         <input type="hidden" name="_method" value="put" />  
  </form>  

test.jsp

<h1> <a href="order/1">get</a><br></h1>
<h1> post:<form action="order/1" method="post">
           <input type="submit" value="post">
</form></h1>
  <h1> delete : <form action="order/1" method="post">
      <input type="hidden" name="_method" value="delete">
            <input type="submit" value="delete">
        </form>
  </h1>
<h1>  <span onclick="a()">p ut</span>
         <form action="order/1" method="post" id="myform">
              <input type="hidden" name="_method" value="put">
         </form></h1>

<script type="text/javascript">
    function a(){
         var form2=document.getElementById("myform");
         form2.submit();
    }
</script>

test.java

@Controller
public class OrderController {

    @RequestMapping(value = "order/{id}",method = RequestMethod.GET)
      public String get(@PathVariable("id") int id){
          System.out.println("get......");
          return "get";
      }
    @RequestMapping(value = "order/{id}",method = RequestMethod.POST)
    public String post(@PathVariable("id") int id){
        System.out.println("post......");
        return "post";
    }
    @RequestMapping(value = "order/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") int id){
        System.out.println("delete......");
        return "delete";
    }
    @RequestMapping(value = "order/{id}",method = RequestMethod.PUT)
    public String put(@PathVariable("id") int id){
        System.out.println("put......");
        return "put";
    }




}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值