2012-03-23 64 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上面的帖子,因为它帮助我们找到了这个小错字。

这不像是你的问题,但对于其他人谁被卡住试图弄清楚为什么“删除”,不支持......

相关问题