Spring REST DELETE示例错误

时间:2011-08-24 07:39:40

标签: spring rest http curl http-delete

我想实现Spring REST方法。我试过Get并且它有效:

@RequestMapping(value = "{systemId}", method = RequestMethod.GET)
    public
    @ResponseBody
    Configuration getConfigurationInJSON(HttpServletResponse response, @PathVariable long systemId) throws IOException {
        if (configurationMap.containsKey(systemId)) {
            return configurationMap.get(systemId);
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return null;
        }
    }

但是,当我想实现DELETE时,它没有。我的方法是:

@RequestMapping(value = "{systemId}", method = RequestMethod.DELETE)
    public void deleteConfiguration(HttpServletResponse response, @PathVariable long systemId) throws IOException {
        if (configurationMap.containsKey(systemId)) {
            configurationMap.remove(systemId);
            response.setStatus(HttpServletResponse.SC_FOUND);
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }

    }

我的web.xml如下:

        MVC-调度         org.springframework.web.servlet.DispatcherServlet         1     

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/ui/*</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

当我尝试使用Curl for GET时有效的命令:

curl http://localhost:8080/ui/webapp/conf/1

当我尝试使用Curl for DELETE时,该命令不起作用:

curl -x delete http://localhost:8080/ui/webapp/conf/1

第二个命令在curl后一段时间后发出错误:

curl: (7) couldn't connect to host

Tomcat端没有错误,在调试时它没有进入删除方法体。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

尝试

curl -X DELETE http://localhost:8080/ui/webapp/conf/1

大写DELETE和大写X