gpt4 book ai didi

Spring MVC注解 Controller 方法,无法进行DELETE操作的 "find"方法

转载 作者:IT老高 更新时间:2023-10-28 13:47:21 24 4
gpt4 key购买 nike

下面是实际代码:

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

向/competitors/200 发送 DELETE 请求会导致错误:

“HTTP 状态 405 - 不支持请求方法 'DELETE'”

来自 Spring 的日志记录确认找不到此方法的路由:

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 'com.gtspt.vrsboserver.controllers.CompetitorController@4fe7f80' 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 [com.gtspt.vrsboserver.controllers.CompetitorController@4fe7f80]: 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 [com.gtspt.vrsboserver.controllers.CompetitorController@4fe7f80]: 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.gtspt.vrsboserver.controllers.CompetitorController@4fe7f80]: 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?”。

最佳答案

我有同样的问题。有什么帮助,它可能不是最终的解决方案,但对我有用:

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参数是这样通过header传递的(客户端代码-不完整):

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpDelete httpDelete = new HttpDelete...

...

httpDelete.setHeader("id", "123");

...

httpClient.execute(httpDelete);

我正在使用 Apache HttpClient。

关于Spring MVC注解 Controller 方法,无法进行DELETE操作的 "find"方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9839693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com