Announcement Announcement Module
Collapse
No announcement yet.
NO PUT and DELETE Support Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NO PUT and DELETE Support

    Hi,

    it seems that my server doesn't support the PUT and DELETE-Method and i cant fix it.

    The error at the client:
    Code:
    UPDATE
    01.06.2011 09:51:14 org.springframework.web.client.RestTemplate handleResponseError
    WARNUNG: PUT request for "http://localhost:8080/core/users/patients/26" resulted in 405 (Request method 'PUT' not supported); invoking error handler
    I/O error: Server returned HTTP response code: 405 for URL: http://localhost:8080/core/users/patients/26; nested exception is java.io.IOException: Server returned HTTP response code: 405 for URL: http://localhost:8080/core/users/patients/26
    DELETE
    01.06.2011 09:51:14 org.springframework.web.client.RestTemplate handleResponseError
    WARNUNG: DELETE request for "http://localhost:8080/core/users/patients/26" resulted in 405 (Request method 'DELETE' not supported); invoking error handler
    I/O error: Server returned HTTP response code: 405 for URL: http://localhost:8080/core/users/patients/26; nested exception is java.io.IOException: Server returned HTTP response code: 405 for URL: http://localhost:8080/core/users/patients/26
    The error at the server:
    Code:
    2011-06-01 09:51:14.867:INFO:/core:org.tuckey.web.filters.urlrewrite.utils.ServerNameMatcher DEBUG: looking for hostname match on current server name localhost
    2011-06-01 09:51:14.867:INFO:/core:org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: checking for status path on /core/users/patients/26
    2011-06-01 09:51:14.868:INFO:/core:org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: urlRewriter engine not loaded ignoring request (could be a conf fil
    e problem)
    <2011-06-01 09:51:14> DEBUG : DispatcherServlet servlet.DispatcherServlet : DispatcherServlet with name 'DispatcherServlet' processing DELETE request for [/core
    /users/patients/26]
    <2011-06-01 09:51:14> DEBUG : AbstractUrlHandlerMapping annotation.DefaultAnnotationHandlerMapping : Matching patterns for request [/users/patients/26] are [/us
    ers/patients/{id}]
    <2011-06-01 09:51:14> DEBUG : AbstractUrlHandlerMapping annotation.DefaultAnnotationHandlerMapping : URI Template variables for request [/users/patients/26] are
     {id=26}
    <2011-06-01 09:51:14> DEBUG : AbstractUrlHandlerMapping annotation.DefaultAnnotationHandlerMapping : Mapping [/users/patients/26] to HandlerExecutionChain with
    handler [de.luebeck.uni.itm.mss.controller.user.PatientController@a40787] and 4 interceptors
    <2011-06-01 09:51:14> DEBUG : AbstractHandlerExceptionResolver annotation.AnnotationMethodHandlerExceptionResolver : Resolving exception from handler [de.luebec
    k.uni.itm.mss.controller.user.PatientController@a40787]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported
    <2011-06-01 09:51:14> DEBUG : AbstractHandlerExceptionResolver annotation.ResponseStatusExceptionResolver : Resolving exception from handler [de.luebeck.uni.itm
    .mss.controller.user.PatientController@a40787]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported
    <2011-06-01 09:51:14> DEBUG : AbstractHandlerExceptionResolver support.DefaultHandlerExceptionResolver : Resolving exception from handler [de.luebeck.uni.itm.ms
    s.controller.user.PatientController@a40787]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported
    <2011-06-01 09:51:14> WARN  : DefaultHandlerExceptionResolver servlet.PageNotFound : Request method 'DELETE' not supported
    <2011-06-01 09:51:14> DEBUG : DispatcherServlet servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'DispatcherServlet': assum
    ing HandlerAdapter completed request handling
    <2011-06-01 09:51:14> DEBUG : FrameworkServlet servlet.DispatcherServlet : Successfully completed request
    my web.xml
    Code:
     	<display-name>spring-3.0-rest</display-name>
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/META-INF/*.xml</param-value>
    	</context-param>
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	
    	<servlet-mapping>
    		<servlet-name>default</servlet-name>
    		<url-pattern>/static/*</url-pattern>
    	</servlet-mapping>
    	
    	<servlet>
    		<servlet-name>DispatcherServlet</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value></param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	
    	<servlet-mapping>
    		<servlet-name>DispatcherServlet</servlet-name>
    		<url-pattern>/*</url-pattern>
    	</servlet-mapping>
    	
    	<!-- URL rewrite filter, used to access the static resources to http://localhost/static/foo.css http://localhost/foo.css -->
    	<filter>
    		<filter-name>UrlRewriteFilter</filter-name>
    		<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    		<init-param>
    			<param-name>confReloadCheckInterval</param-name>
    			<param-value>60</param-value>
    		</init-param>
    		<init-param>
    			<param-name>logLevel</param-name>
    			<param-value>DEBUG</param-value>
    		</init-param>           
    	</filter>
    	
    	<filter-mapping>
    		<filter-name>UrlRewriteFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    
     	<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>DispatcherServlet</servlet-name>
    	</filter-mapping>
    	
    </web-app>
    myServer.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    	<!-- Enable annotation scanning. -->
    	<context:component-scan base-package="de.luebeck.uni.itm.mss" />
    
    	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    		<property name="messageConverters">
    			<list>
    				<ref local="jsonConverter" />
    				<ref local="xmlMarshallingConverter" />
    			</list>
    		</property>
    	</bean>
    	
    	<!-- Define the JSON marshaller which is used to convert the Objects <-> JSON. -->
    	<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    		<property name="supportedMediaTypes" value="application/json" />
    	</bean>
    
    	<!-- Define the OXM marshaller which is used to convert the Objects <-> XML. -->
    	<bean id="xmlMarshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    		<property name="supportedMediaTypes" value="application/xml" />
    		<property name="unmarshaller" ref="xmlMarshaller" />
    		<property name="marshaller" ref="xmlMarshaller" />
    	</bean>
    
    	<!--marshallers -->
    	<bean id="xmlMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
    		<property name="useAttributeForTypes">
    			<list>
    				<value>java.lang.Long</value>
    				<value>java.lang.String</value>
    				<value>java.util.Date</value>
    				<value>java.util.Locale</value>
    			</list>
    		</property>
    	</bean>
    
    	<!-- <bean id="oxmMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/> 
    		<bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> 
    		<property name="supportedMediaTypes" value="application/xml" /> <property 
    		name="marshaller" ref="oxmMarshaller" /> <property name="unmarshaller" ref="oxmMarshaller" 
    		/> </bean> Define the JSON marshaller which is used to convert the Objects 
    		<-> JSON. <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
    		<property name="supportedMediaTypes" value="application/json" /> </bean> 
    		Required for REST services in order to bind the return value to the ResponseBody. 
    		<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    		<property name="messageConverters"> <util:list id="beanList"> <ref bean="jsonConverter" 
    		/> <ref bean="marshallingHttpMessageConverter" /> </util:list> </property> 
    		</bean> -->
    
    	<!-- Should be defined last! -->
    	<mvc:annotation-driven />
    
    </beans>
    my Controller:
    Code:
    @Controller
    @RequestMapping("/users/patients")
    @Transactional
    public class PatientController{
    	
    	private IPatientRepository repository;
    
    	public PatientController() {
    		super();
    //...
        @RequestMapping(value="/{id}",
        				method=RequestMethod.DELETE,
        				headers = "Accept=application/json, application/xml")
        @ResponseBody
        public ModelAndView delete(	@PathVariable Integer id) {
        		repository.remove(id);
                return new ModelAndView("redirect:home");
        }
    }
    My Client:
    Code:
    	public static void main(String[] args) {
    		//...
    		//DELETE
    		String url = "http://localhost:8080/core";
    		String cmd = "/users/patients";
    		RestTemplate template = RESTTemplateConnection.REST_TEMPLATE;
    		System.out.println("DELETE");
    		try{
    			delete(template,url,cmd,"/{id}",id);
    		}catch (Exception e) {
    			System.out.println(e.getLocalizedMessage());
    		}
    	}
    	private static void delete(RestTemplate template, String url, String cmd, String resource, Integer id){
    		template.delete(url+cmd+resource,id);
    	}
    Can u help me pls?

    Greetings
    MK

  • #2
    Get a different server? Whether the server supports PUT/DELETE or not cannot be solved by a REST client framework. There are a number of work-arounds, all rather ugly but they involve such things as using POST with an "action" command: http://.../delete-contact?first-name...ast-name=Smith

    Comment


    • #3
      The link doesn't work!
      Every Spring shows an example which includes a PUT and a DELETE method... But always without the workaround... This is bad... But why is it so ugly? Does exist other alternatives?

      Comment


      • #4
        No, the link was an example of a workaround where PUT/DELETE are not available. You override the meaning of POST by placing "action" commands within your post such as "add" for PUT and "delete" for DELETE.

        Comment


        • #5
          Do you mean something like this:

          Code:
              @RequestMapping(value="/delete/{id}",
              				method=RequestMethod.POST,
              				headers = "Accept=application/json, application/xml")
              @ResponseBody
              public ModelAndView delete(	@PathVariable Integer id) {
              		repository.remove(id);
                      return new ModelAndView("redirect:home");
              }
          This is so dirty
          If not, can you show me an example pls?
          I am a Spring beginner but i wonder why RequestMethod.DELETE and PUT exists?

          Comment


          • #6
            I agree, it is not very RESTful but you are correct, the API would look something like your example. One of the responsibilities of an architect/developer is to make compromises when necessary in order to meet customer commitments.

            RequestMethod.DELETE and PUT exist for those application servers that support those HTTP verbs. You just happen to be using one that does not support those verbs. So your choice is compromise your design or move to an application server that supports HTTP PUT and DELETE in addition to GET/POST. Such a server change may just be an upgrade to a new version.

            Comment


            • #7
              If your Server is Tomcat

              Tomcat by default is not enabled for HTTP PUT command. But, it can be easily configure to support it.
              Consider this for activating DELETE and PUT
              http://www.codereye.com/2010/12/conf...-http-put.html

              If you are not using Tomcat - since any Server would be handling these potentially dangerous methods in the same way I would try something like this anyway.

              Comment

              Working...
              X