Announcement Announcement Module
Collapse
No announcement yet.
Request method 'PUT' not supported Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Request method 'PUT' not supported

    i try to make a very simple demo for spring3.0 rest mvc webapp.
    The GET,POST,DELETE three http methods work well, but PUT method not.
    exception:
    Code:
    Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'uncaughtException'; model is {exception=org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported}
    org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.resolveHandlerMethod(AnnotationMethodHandlerAdapter.java:567)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:409)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
    	at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:574)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:640)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    Controller code:
    Code:
    	@RequestMapping(value="/{id}/update", method = RequestMethod.GET)
    	public String getUpdateForm(@PathVariable int id, Model model) {	
    		User user=userService.findById(id);
    		model.addAttribute("user", user);
    		return "user/updateForm";
    	}
    	
    	@RequestMapping(value="/{id}", method = RequestMethod.PUT)
    	public String update(@Valid User user,BindingResult result, Model model) {	
    		if (result.hasErrors()){
    			return "user/updateForm";
    		}else{
    	        userService.update(user);
    	        model.addAttribute("statusMessageKey", "user.form.msg.success");
    			return LIST_ACTION ;
    		}
    	}
    SpringDemo-servlet.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    	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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
    	<mvc:annotation-driven />
    
    	<context:component-scan base-package="web.controller">
    		<context:include-filter type="annotation"
    			expression="org.springframework.stereotype.Controller" />
    	</context:component-scan>
    
    	<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    		<property name="exceptionMappings">
    			<props>
    				<prop key=".DataAccessException">dataAccessFailure</prop>		
    				<prop key=".lang.Exception">uncaughtException</prop>		
    				<prop key=".AuthenticationCredentialsNotFoundException">login</prop>		
    				<prop key=".AccessDeniedException">login</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    		p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="2"/>
    
    	<bean id="multipartResolver"
    		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    		<!-- one of the properties available; the maximum file size in bytes -->
    		<property name="maxUploadSize" value="100000" />
    	</bean>
    	
    	<bean id="messageSource"
    		class="org.springframework.context.support.ResourceBundleMessageSource"
    		p:basename="messages" />
    </beans>
    web.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	id="WebApp_ID" version="2.5">
    	<display-name>SpringDemo</display-name>
    	<description>SpringDemo</description>
    
    	<context-param>
    		<param-name>log4jConfigLocation</param-name>
    		<param-value>classpath:log4j.properties</param-value>
    	</context-param>
    
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    			classpath:web/persist/UserServiceTest-context.xml
    		</param-value>
    	</context-param>
    
    	<listener>
    		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    	</listener>
    
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    
    	<listener>
    		<listener-class>web.controller.InitDbListener</listener-class>
    	</listener>	
    
    	<servlet>
    		<servlet-name>SpringDemo</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>SpringDemo</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    	
    	<servlet-mapping>
    		<servlet-name>default</servlet-name>
    		<url-pattern>/static/*</url-pattern>
    	</servlet-mapping>	
    
    	<!-- required to enable Spring 3.0 REST support -->
    	<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>SpringDemo</servlet-name>
    	</filter-mapping>
    
    	<session-config>
    		<session-timeout>10</session-timeout>
    	</session-config>
    
    	<welcome-file-list>
    		<welcome-file>index.jsp</welcome-file>
    	</welcome-file-list>
    
    	<error-page>
    		<exception-type>java.lang.Exception</exception-type>
    		<location>/WEB-INF/jsp/uncaughtException.jsp</location>
    	</error-page>
    </web-app>
    Thanks.

  • #2
    Are you sure that all your jars are Spring 3.0. This was an exception thrown by the AnnotationMethodHandlerAdapter from spring 2.5.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      Are you sure that all your jars are Spring 3.0. This was an exception thrown by the AnnotationMethodHandlerAdapter from spring 2.5.
      yes. all spring-jar are 3.0.
      but all dependencies are copy from spring 2.5-with-dependencies.
      can it make wrong?

      Comment


      • #4
        The previous excpetion is not complete.
        And the complete exception output:
        Code:
        08:23:30,867 DEBUG org.springframework.web.servlet.DispatcherServlet:690 - DispatcherServlet with name 'SpringDemo' processing PUT request for [/springDemo/users/3/update]
        08:23:30,867 DEBUG web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping:263 - Matching patterns for request [/users/3/update] are [/users/{id}/update]
        08:23:30,867 DEBUG web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping:219 - Mapping [/users/3/update] to handler 'web.controller.UserController@24c414'
        08:23:30,867 DEBUG springframework.web.servlet.handler.SimpleMappingExceptionResolver:106 - Resolving exception from handler [web.controller.UserController@24c414]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
        08:23:30,867 DEBUG springframework.web.servlet.handler.SimpleMappingExceptionResolver:202 - Resolving to view 'uncaughtException' for exception of type [org.springframework.web.HttpRequestMethodNotSupportedException], based on exception mapping [.lang.Exception]
        08:23:30,867 DEBUG springframework.web.servlet.handler.SimpleMappingExceptionResolver:294 - Exposing Exception as model attribute 'exception'
        08:23:30,867 DEBUG org.springframework.web.servlet.DispatcherServlet:1014 - Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'uncaughtException'; model is {exception=org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported}
        org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
        	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.resolveHandlerMethod(AnnotationMethodHandlerAdapter.java:567)
        	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:409)
        	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
        	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
        	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
        	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
        	at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:574)
        	at javax.servlet.http.HttpServlet.service(HttpServlet.java:640)
        	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:68)
        	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
        	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        	at java.lang.Thread.run(Thread.java:619)
        08:23:30,867 DEBUG springframework.beans.factory.support.DefaultListableBeanFactory:1444 - Invoking afterPropertiesSet() on bean with name 'uncaughtException'
        08:23:30,867 DEBUG org.springframework.web.servlet.DispatcherServlet:1058 - Rendering view [org.springframework.web.servlet.view.JstlView: name 'uncaughtException'; URL [/WEB-INF/jsp/uncaughtException.jsp]] in DispatcherServlet with name 'SpringDemo'
        08:23:30,867 DEBUG springframework.web.servlet.view.JstlView:328 - Added model object 'exception' of type [org.springframework.web.HttpRequestMethodNotSupportedException] to request in view with name 'uncaughtException'
        08:23:30,867 DEBUG springframework.web.servlet.view.JstlView:237 - Forwarding to resource [/WEB-INF/jsp/uncaughtException.jsp] in InternalResourceView 'uncaughtException'
        08:23:31,039 DEBUG org.springframework.web.servlet.DispatcherServlet:677 - Successfully completed request

        Comment


        • #5
          I'm a bit of a newbie so sorry if this is a really silly question, but should this:

          Code:
          @RequestMapping(value="/{id}", method = RequestMethod.PUT)
          be this:
          Code:
          @RequestMapping(value="/{id}/update", method = RequestMethod.PUT)
          ?

          PUK

          Comment


          • #6
            Originally posted by PUK_999 View Post
            I'm a bit of a newbie so sorry if this is a really silly question, but should this:

            Code:
            @RequestMapping(value="/{id}", method = RequestMethod.PUT)
            be this:
            Code:
            @RequestMapping(value="/{id}/update", method = RequestMethod.PUT)
            ?

            PUK
            in rest style.
            Code:
            url                                        http method          action
            http://host/app/users                 get                list users
            http://host/app/users/new          get                show create user form
            http://host/app/users/1              get                show user[id=1]
            http://host/app/users/1/update    get                show edit form for user[id=1]
            http://host/app/users/1              put                submit the edit form
            so
            Code:
            @RequestMapping(value="/{id}", method = RequestMethod.PUT)
            means submit the edit form

            Comment


            • #7
              Originally posted by PUK_999 View Post
              I'm a bit of a newbie so sorry if this is a really silly question, but should this:

              Code:
              @RequestMapping(value="/{id}", method = RequestMethod.PUT)
              be this:
              Code:
              @RequestMapping(value="/{id}/update", method = RequestMethod.PUT)
              ?

              PUK
              Not if you're following a true REST interface.

              Comment


              • #8
                The stack trace posted mentioned the action was "/users/3/update" which will not map against /{id}.

                Comment


                • #9
                  Originally posted by PUK_999 View Post
                  The stack trace posted mentioned the action was "/users/3/update" which will not map against /{id}.
                  In rest style,visiting the url '/users/3' by http method 'PUT' means that submit the edit form for user[3].

                  The stack trace mentioned 'Request method 'PUT' not supported' ,not wrong map.

                  Comment


                  • #10
                    Code:
                        @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
                        public String delete(@PathVariable int id) {
                            userService.delete(new User(id));
                            return LIST_ACTION;
                        }
                    can execute correctly. it means the server insist rest method.

                    I feel very confused about the PUT method wrong.

                    The Complete code
                    Code:
                    @Controller
                    @RequestMapping(value="/users")
                    public class UserController {	
                    	private UserService userService;
                    
                    	
                    	private final String LIST_ACTION = "redirect:/users";  
                    	
                    	@RequestMapping(method = RequestMethod.GET)
                    	public String get(Model model) {
                    		model.addAttribute("users", userService.findAll());
                    		return "user/list";
                    	}
                        
                    	@RequestMapping(value="/{id}", method = RequestMethod.GET)
                    	public String getById(@PathVariable int id, Model model) {
                    		model.addAttribute("user",userService.findById(id));
                    		return "user/show";
                    	}
                    	
                    	@RequestMapping(value="/new", method = RequestMethod.GET)
                    	public String getNewForm(Model model) {	
                    		model.addAttribute("user", new User());
                    		return "user/newForm";
                    	}	
                    
                    	@RequestMapping(method = RequestMethod.POST)
                    	public String add(@Valid User user,BindingResult result,Model model) {	
                    		if (result.hasErrors()){
                    				return "user/newForm";
                    		}else{	
                    	        userService.insert(user);
                    	        model.addAttribute("statusMessageKey", "user.form.msg.success");
                    			return LIST_ACTION ;
                    		}
                    	}
                    
                    	@RequestMapping(value="/{id}/update", method = RequestMethod.GET)
                    	public String getUpdateForm(@PathVariable int id, Model model) {	
                    		User user=userService.findById(id);
                    		model.addAttribute("user", user);
                    		return "user/updateForm";
                    	}
                    	
                    	@RequestMapping(value="/{id}", method = RequestMethod.PUT)
                    	public String update(@Valid User user,BindingResult result, Model model) {	
                    		if (result.hasErrors()){
                    			return "user/updateForm";
                    		}else{
                    	        userService.update(user);
                    	        model.addAttribute("statusMessageKey", "user.form.msg.success");
                    			return LIST_ACTION ;
                    		}
                    	}
                    
                        @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
                        public String delete(@PathVariable int id) {
                            userService.delete(new User(id));
                            return LIST_ACTION;
                        }
                    
                        
                    	@Autowired
                    	public void setUserService(UserService userService) {
                    		this.userService = userService;
                    	}	
                    }

                    Comment


                    • #11
                      Usually what this error means is that there is not a method in the controller that will handle a PUT request for the url mapping being supplied. I think PUK_999 was onto the right path. I'd double check the path in the request url.

                      Comment


                      • #12
                        Originally posted by jonsinfinity View Post
                        Usually what this error means is that there is not a method in the controller that will handle a PUT request for the url mapping being supplied. I think PUK_999 was onto the right path. I'd double check the path in the request url.
                        Yes,you are right. Thanks.
                        And Thanks PUK_999.

                        Comment

                        Working...
                        X