---
title: "HiddenHttpMethodFilter：自动转换Http Method"
author: "andyao"
date: "2009-03-29T11:21:43Z"
tags: ["Blog", "HTML", "REST", "Spring", "Web"]
categories: ["企业架构"]
slug: "hiddenhttpmethodfilter-http-method"
draft: false
lastmod: "2009-03-29T11:23:28Z"
id: 356942
url: "https://andyao.iteye.com/blog/356942"
---

REST的核心之一就是提供统一接口，也就是说对所有的资源（URLs）都可以通过Http中定义的GET，POST，PUT，DELETE方法进行操作。但是html只支持GET和POST，可以采用以下方法解决这个问题：

使用Javascripts进行PUT和DELETE
使用REST－RPC方式，在url中指明方法; 比如发GET请求至/blogs/new，创建一个新的blog
通过重载post来提供real method。在post域中添加hidden fields来指明实际的方法。
在spring 3.0中提供了一个可以自动转换http method的filter－[HiddenHttpMethodFilter](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/filter/HiddenHttpMethodFilter.html)。原理很简单，HiddenHttpMethodFilter拦截request，并且将hidden field中的real method设置到当前request的header中，在controller中按标准的RESTFul风格处理即可。很简单，自己实现一个这种filter也不难。

这样就可以很好的解决PUT和DELETE方法问题。美中不足的就是多加了一个hidden fileds，有一些bad small。