Note that there are some explanatory texts on larger screens.

plurals
  1. POSpringMVC 3.2.4 Request method 'PUT' not supported
    primarykey
    data
    text
    <p>Just started working with Java and Spring, coming from a C# background and I am having trouble getting a 'PUT' request to work.</p> <p>I am on Spring 3.2.4 running on Jetty 9.0.6.</p> <p>I have a simple controller with the following method</p> <pre><code>@RequestMapping(value = "/{id}", method = RequestMethod.PUT) public ResponseEntity&lt;String&gt; update(@PathVariable Integer id, @RequestBody Employee employee) { HttpHeaders headers = new HttpHeaders(); headers.set("content-location", "/api/employees/" + id); return new ResponseEntity&lt;&gt;("", headers, HttpStatus.OK); } </code></pre> <p>When this request is executed, I get the following error:</p> <blockquote> <p>Oct 31, 2013 10:16:16 AM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpRequestMethodNotSupported </p> <p>WARNING: Request method 'PUT' not supported</p> </blockquote> <p>If I change the RequestMethod to 'POST' it works fine.</p> <p>Does Spring support 'PUT' requests? How do I get it to recognize the 'PUT'</p> <p>Joe</p> <p><strong>EDIT</strong></p> <p>Turns out I was being stupid. Affe clued me in when he mentioned the url. I was accessing it like 'api/employees?id=32' when it should have been 'api/employees/32'</p> <p>In the hopes this helps someone else, here is the web descriptor, the servlet and the controller.</p> <h3>Web Descriptor</h3> <pre><code>&lt;web-app xmlns='http://java.sun.com/xml/ns/javaee' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd' version='3.0'&gt; &lt;display-name&gt;timesheet-app&lt;/display-name&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <h3>MVC-Dispatcher-Servlet</h3> <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt; &lt;beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:context='http://www.springframework.org/schema/context' xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd'&gt; &lt;!-- Scans the classpath of this application for @Components to deploy as beans --&gt; &lt;context:component-scan base-package='org.timesheets.web' /&gt; &lt;!-- Configures the @Controller programming model --&gt; &lt;mvc:annotation-driven /&gt; &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory --&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --&gt; &lt;bean class='org.springframework.web.servlet.view.InternalResourceViewResolver'&gt; &lt;property name='prefix' value='/WEB-INF/views/' /&gt; &lt;property name='suffix' value='.jsp' /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <h3>Controller</h3> <pre><code>@Controller @RequestMapping("/api/employees") public class EmployeeController { @RequestMapping(method = RequestMethod.GET) public @ResponseBody List&lt;Employee&gt; get() { List&lt;Employee&gt; employees = new ArrayList&lt;&gt;(); for(int i = 1; i &lt;= 10; i++){ employees.add(new Employee(i, "Test " + i, "Department " + i)); } return employees; } @RequestMapping(value = "/{id}", method = RequestMethod.GET) public @ResponseBody Employee get(@PathVariable Integer id) { return new Employee(1, "Test", "IT"); } @RequestMapping(method = RequestMethod.POST) public ResponseEntity&lt;String&gt; create(@RequestBody Employee employee) { HttpHeaders headers = new HttpHeaders(); headers.set("content-location", "/api/employees/32"); return new ResponseEntity&lt;&gt;("", headers, HttpStatus.CREATED); } @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public ResponseEntity&lt;String&gt; update(@PathVariable Integer id, @RequestBody Employee employee) { HttpHeaders headers = new HttpHeaders(); headers.set("content-location", "/api/employees/" + id); return new ResponseEntity&lt;&gt;("", headers, HttpStatus.OK); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload