2012-03-23 80 views
11

下面是實際的代碼:彈簧MVC註釋控制器方法,無法「查找」方法DELETE操作

@RequestMapping(value = "/competitors/{id}", method = RequestMethod.GET) 
public Competitor getCompetitor(@PathVariable("id") long id) 
{ 
    Competitor competitor = competitorService.getCompetitorById(id); 

    if (null == competitor) 
    { 
     EmptyResultDataAccessException e = new EmptyResultDataAccessException(1); 
     logger.log(Level.WARN, e.getMessage()); 
     throw e; 
    } 

    return competitor; 
} 

@RequestMapping(value = "/competitors/{id}", method = RequestMethod.DELETE) 
public String deleteCompetitor(@PathVariable("id") long id) 
{ 
    Competitor competitor = new Competitor(); 
    competitor.setId(id); 
    competitorService.deleteCompetitor(competitor); 

    return "Solid gone!"; 
} 

發送一個DELETE請求到/競爭對手/ 200個結果中的錯誤:

「 HTTP狀態405 - 請求方法「刪除」不支持」

從春天的記錄證實,這種方法沒有任何途徑可以發現:

5559 [tomcat-http--3] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing DELETE request for [/vrsboserver/competitors/200] 5562 [tomcat-http--3] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping 
- Matching patterns for request [/competitors/200] are [/competitors/{id}] 5565 [tomcat-http--3] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping 
- Mapping [/competitors/200] to handler '[email protected]' 5565 [tomcat-http--3] DEBUG org.springframework.web.servlet.mvc.WebContentInterceptor - Looking up cache seconds for [/competitors/200] 5565 [tomcat-http--3] DEBUG org.springframework.web.servlet.mvc.WebContentInterceptor - Applying default cache seconds to [/competitors/200] 5566 [tomcat-http--3] DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver 
- Resolving exception from handler [[email protected]]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported 5567 [tomcat-http--3] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver 
- Resolving exception from handler [[email protected]]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported 5568 [tomcat-http--3] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver 
- Resolving exception from handler [com.g[email protected]]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported 5568 [tomcat-http--3] WARN org.springframework.web.servlet.PageNotFound - Request method 'DELETE' not supported 

我的回答是「BUH?」。

+0

是否所有的春瓶相同版本的? – 2012-03-23 13:14:11

+0

可以肯定的是,你已經驗證GET可以在同一個映射上工作? – smp7d 2012-03-23 13:19:18

+0

GET絕對有效。 Maven使用「$ {org.springframework.version}」作爲每個版本字段來獲得我的Spring jar,所以它們絕對匹配。 – Archeus 2012-03-23 13:20:58

回答

0

嘗試將其更改爲method = RequestMethod.GET並查看它是否有效。

+0

具有GET簽名的方法已經存在(請參閱已發佈的代碼)並且可行。 – Archeus 2012-03-23 13:07:59

+0

你如何告訴瀏覽器發出刪除請求?該標題集在哪裏?就像這個鏈接一樣:http://www.ibm.com/developerworks/webservices/library/wa-restful/index.html?ca=drs- – duffymo 2012-03-23 13:11:16

+0

你在表單中使用method = DELETE嗎?我很好奇,因爲我真的不知道。 – duffymo 2012-03-23 13:14:20

1

普通瀏覽器只支持get/post。

<filter> 
    <filter-name>httpMethodFilter</filter-name> 
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>httpMethodFilter</filter-name> 
    <servlet-name>springDispatcher</servlet-name> 
</filter-mapping> 
4

我有同樣的問題。有什麼幫助,它可能不是最終的解決方案,但正在爲我工​​作:

更改註釋和方法的參數deleteCompetitors。刪除id(方法參數)。從HttpServletRequest讀取參數id

@RequestMapping(value = "/competitors", method = RequestMethod.DELETE) 
public String deleteCompetitor(HttpServletRequest request) 
{ 
    String idHeader = request.getHeader("id"); 

    Integer id = Integer.valueOf(idHeader).intValue(); 

    Competitor competitor = new Competitor(); 
    competitor.setId(id); 
    competitorService.deleteCompetitor(competitor); 

    return "Solid gone!"; 
} 

id參數由頭通過這種方式(客戶端的代碼 - 不完整):

DefaultHttpClient httpClient = new DefaultHttpClient(); 

HttpDelete httpDelete = new HttpDelete... 

... 

httpDelete.setHeader("id", "123"); 

... 

httpClient.execute(httpDelete); 

我使用了Apache的HttpClient。

0

最近我遇到了這個問題。這裏有幾個我發現/評論:

我運行Tomcat 7.0.42與Spring 3.2.2

以下消息吐在日誌中的所有的這些情況。 405方法不允許返回給客戶端。

org.springframework.web.servlet.PageNotFound - Request method 'DELETE' not supported 
  1. 其餘URL你使用是錯誤的。即使端點不在,您仍然會得到一個405.
  2. 您尚未登錄並且未被授權執行任何操作,更不用說執行DELETE
  3. 實際上不支持DELETE,因爲沒有函數使用方法= RequestMethod.GET
  4. Tomcat阻止像DELETE,PUT等操作由於只讀INIT-PARAM設置爲true
  5. 的方法是本,刪除被允許,一切都很好,除了有一個未捕獲的運行時異常(例如空指針異常)的方法中

,不同之處3和4,顯示的信息和迴應是非常誤導。它會把你調查下來的兔子洞,結果無果而終。

什麼結束了我的問題是,我們有這樣的方法:

public void deleteSomething(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") long id, @RequestParam String objectName); 

它應該是這樣的:

public void deleteSomething(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") long id, @RequestParam("objectName") String objectName); 

看到區別?它是@RequestParam之後的缺失(「objectName」)。它在STS中編譯並運行良好,但是直接部署在tomcat服務器上時,它不起作用。

感謝@fmelan上面的帖子,因爲它幫助我們找到了這個小錯字。

這不像是你的問題,但對於其他人誰被卡住試圖弄清楚爲什麼「刪除」,不支持......