Tomcat,启用删除方法

Tomcat, enable DELETE method

这个问题在How to make Apache Tomcat accept DELETE method之前有人问过,但是它提供的解决方案对我不起作用。我添加了

<init-param>
    <param-name>readonly</param-name>
    <param-value>false</param-value>
</init-param>

到 web.xml 我的 <servlet> 设置如下所示:

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>readonly</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

但是,我在访问 DELETE 时仍然得到 405。有什么建议吗?顺便说一句,我已经重新启动 tomcat.

查看源代码: Default Servlet

在那里你可以看到,如果网络资源的删除调用失败,会返回一个状态码405 Not allowed

从你的问题来看,不清楚你要删除的是什么——你的资源似乎无法删除。

tomcat funcspec 状态:

On each HTTP DELETE request processed by this servlet, the following >processing shall be performed:

  • If modifications to the static resources are not allowed (set by a configuration parameter), return HTTP status 403 (forbidden).
  • If an attempt is made to delete a resource from /META-INF or /WEB-INF, return HTTP status 403 (forbidden).
  • If the requested resource does not exist, return HTTP status 404 (not found)
  • Unbind the resource from the directory context containing the static resources for this web application. If successful, return HTTP status 204 (no content). Otherwise, return HTTP status 405 (method not allowed).

无论是源代码还是功能规范,您都可以看到删除方法已被 Servlet 接受,您只需将 URL 传递给实际可删除的资源即可。