我想实现 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-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>
当我尝试使用 Curl for 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端没有错误,调试时也没有进入delete方法体。
有任何想法吗?