3

I'm trying to use RequestMethod.PUT and RequestMethod.DELETE in Spring MVC controller. My controller has a @RequestMapping for PUT and DELETE, but despite that, I'm getting error when trying to use it:

Request method 'GET'/'POST' not supported

Controller:

@RequestMapping(value = "/admin/user/{id}", method = RequestMethod.PUT)
public ModelAndView updateUser(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("user") User user, @PathVariable long id) {
    ...
}

@RequestMapping(value = "/admin/user/delete/{id}", method = RequestMethod.DELETE)
public @ResponseBody ModelAndView deleteUser(HttpServletRequest request, HttpServletResponse response, @PathVariable long id) {
    ...
}

I've added also filters in 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>

Spring form for PUT:

<form:form id="registerForm" method="put" action="${user.id}" modelAttribute="user" class="form-horizontal">

Generated HTML:

<form id="registerForm" class="form-horizontal" action="13" method="post"><input type="hidden" name="_method" value="put"/>

I know, that if browser isn't supporting PUT/DELETE then POST/GET is used, but why my controller method is not called?

bartex9
  • 391
  • 3
  • 19
  • Isn't "13" a quite strange form action? – Dirk Lachowski Jan 22 '16 at 11:25
  • No, why? This '13' is added to actual address, so in real it is: `/admin/user/13`. Using POST in this case worked perfectly. – bartex9 Jan 22 '16 at 11:27
  • See also http://stackoverflow.com/questions/8250439/can-spring-mvc-have-request-parameters-for-an-http-put-method-or-must-i-use-pos – Raedwald Jan 22 '16 at 13:09
  • Are you sure that a) your filter is executed and b) you have added the filter config to your `pom.xml` and not your `web.xml`? Where do you see the 'Method not supported' error? – Dirk Lachowski Jan 24 '16 at 13:12
  • Oh, sorry. Of course, filters are in my `web.xml`. I see this error in the browser and in GlassFish Server log. – bartex9 Jan 24 '16 at 13:34
  • If the browser sends POST and GET and you only respond to PUT and DELETE, you will never get called. – DwB Jan 24 '16 at 16:34
  • Just use POST for update and delete – DwB Jan 24 '16 at 16:34
  • Possible duplicate of [Using PUT and DELETE methods in Spring MVC](http://stackoverflow.com/questions/13629653/using-put-and-delete-methods-in-spring-mvc) – DwB Jan 24 '16 at 16:35
  • Ok, I know I can use GET/POST, but shouldn't it also works with PUT/DELETE? Why my browser sends POST/GET? How can I fix that? – bartex9 Jan 24 '16 at 19:25
  • @bartex9 did you solve it ? – mfaisalhyder Mar 09 '16 at 19:17
  • Unfortunately no, I didn't. Couldn't make PUT/DELETE working in my project. – bartex9 Mar 10 '16 at 20:09

0 Answers0