Spring REST DELETE 示例错误

发布于 2024-12-01 06:29:20 字数 2036 浏览 3 评论 0原文

我想实现 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 进行 GET 时有效的命令:

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

当我尝试使用 Curl 进行 DELETE 时不起作用的命令:

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

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

curl: (7) couldn't connect to host

Tomcat 端没有错误,并且在调试时没有错误t 输入删除方法主体。

有什么想法吗?

I want to implement Spring REST methods. I tried Get and it works:

@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;
        }
    }

However when I want to implement DELETE it doesn't. My method is that:

@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);
        }

    }

My web.xml as follows:

mvc-dispatcher
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>

Command that works when I try with Curl for GET:

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

Command that doesn't work when I try with Curl for DELETE:

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

Second command gives that error after a time later at curl:

curl: (7) couldn't connect to host

There is no error at Tomcat side and at debug it doesn't enter the delete method body.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2024-12-08 06:29:20

尝试

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

使用大写 DELETE 和大写 X

Try

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

with uppercase DELETE and uppercase X

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文