2012-07-13 201 views
2

我有一個Spring MVC Web應用程序。在其與是應該從另一個資源刪除資源的按鈕形式:獲取「405 - 調用」method = DELETE時不支持請求方法'GET'

<td> 
    <form action="/product-bases/${productBase.id}/positions/${position.id}" method="DELETE"> 
     <input type="submit" value="delete" /> 
    </form> 
</td> 

我的控制器:

@Controller 
@RequestMapping(value = "/product-bases/{id}/positions") 
public class ProductBasePositionController { 

    @RequestMapping(value = "/{positionId}", method = RequestMethod.DELETE) 
    public ModelAndView delete(@PathVariable Integer productBaseId, @PathVariable Integer positionId) { 

所以理論上服務器應該路由到控制器。但可惜沒有,所以職務;)

我越來越

HTTP Status 405 - Request method 'GET' not supported 

type Status report 

message Request method 'GET' not supported 

description The specified HTTP method is not allowed for the requested resource (Request method 'GET' not supported). 
Apache Tomcat/7.0.19 

很顯然,我沒有得到/位置/ ID定義還沒有,但爲什麼要我,我想現在做一個刪除..

(我也試圖從我的spring-test-mvc框架上運行一個模擬servlet而沒有任何tomcat實現,它給了我一個400 - 錯誤的請求。 )

那麼我在這裏錯過了什麼?

哦,只是爲了削減一些角落:發佈和獲取將適用於其他資源,所以我的其餘設置是好的。

的啓動服務器甚至告訴我:

RequestMappingHandlerMapping [INFO] Mapped "{[/product-bases/{id}/positions/{positionId}],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView our.view.controller.ProductBasePositionController.delete(java.lang.Integer,java.lang.Integer) 

任何人一樣困惑,因爲我?如果不那麼如此,請賜教!

回答

4

形式可以通過僅GETPOST提交(也許還PUT,但我懷疑被廣泛地實現),作爲表單提交需要其中的數據被髮送到服務器的方法。

DELETE方法沒有請求主體,因此不支持在表單操作中指定它。

+0

差不多。瀏覽器現在只支持GET和POST,因此在不支持的情況下將該方法默認更改爲GET。 – 2012-07-13 14:41:53

+0

哇,我不知道那..所以我唯一的選擇似乎把一個隱藏的輸入字段的形式,說name = method value = delete?除了javascrip,我的意思是..因爲我不能通過鏈接發送刪除..似乎有點奇怪,並嘗試開發真正的RESTful不可能?! – Pete 2012-07-13 15:48:43

+0

皮特:見http://www.w3.org/html/wg/tracker/issues/195。您可能想要加入HTML工作組。有關詳細信息,請參閱http://www.w3.org/html/wg/。 – 2012-07-13 16:47:23

1

錯誤消息表明瀏覽器實際上是發送GET請求而不是DELETE請求。

你需要做的是:

  • 檢查網頁瀏覽器上的時間源,並

  • 使用瀏覽器的Web調試,看看有什麼要求實際上URL和方法是。

1

你有沒有在你的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> 

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/view.html#rest-method-conversion

+0

啊,這聽起來也很有希望,星期一會試試..謝謝! – Pete 2012-07-13 18:21:37

+0

不幸的是,我們不能使用過濾技巧,因爲它不會再被測試,因爲我們使用不支持過濾器的spring-test-mvc。 – Pete 2012-07-16 07:14:45

0

在微軟的Azure門戶當我打開驗證/授權我得到這個錯誤的Java應用程序。如果在你的情況下是同樣的問題,請恢復你的行動。

相關問題