Here is the actual code:
@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!";
}
Sending a DELETE request to /competitors/200 results in the error:
"HTTP Status 405 - Request method 'DELETE' not supported"
The logging from Spring confirms that no route to this method can be found:
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
To which my response is "BUH?".
I had same problem. What helps and it's probably not the final solution but is working for me:
Change of annotations and parameters of the method deleteCompetitors. Remove id (method parameter too). Read the id parameter from HttpServletRequest.
@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!";
}
The id parameter is passed by header this way (code of the client - not complete):
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpDelete httpDelete = new HttpDelete...
...
httpDelete.setHeader("id", "123");
...
httpClient.execute(httpDelete);
I'm using the Apache HttpClient.
Normal browser only support get / post.
Spring resolved this by using a hidden parameter, to enable it, add below to your web.xml:
<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<servlet-name>springDispatcher</servlet-name>
</filter-mapping>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With