Passing data in the body of a DELETE request

后端 未结 5 1851
后悔当初
后悔当初 2020-12-14 17:05

I have two Spring MVC controller methods. Both receive the same data in the request body (in the format of an HTLM POST form: version=3&name=product1&

相关标签:
5条回答
  • 2020-12-14 17:27

    Spring boot 1.5.*

    @Bean
    public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
        return new TomcatEmbeddedServletContainerFactory(){
            @Override
            protected void customizeConnector(Connector connector) {
                super.customizeConnector(connector);
                connector.setParseBodyMethods("POST,PUT,DELETE");
            }
        };
    }
    
    0 讨论(0)
  • 2020-12-14 17:40

    for spring boot 2.0+ :

    @Bean
    public TomcatServletWebServerFactory containerFactory() {
        return new TomcatServletWebServerFactory() {
            @Override
            protected void customizeConnector(Connector connector) {
                super.customizeConnector(connector);
                connector.setParseBodyMethods("POST,PUT,DELETE");
            }
        };
    }
    
    0 讨论(0)
  • 2020-12-14 17:43

    You can try adding the annotation @RequestBody to your Product argument.

    But if you just need to pass version information, using a request param is more appropriate.

    So add a new argument in your delete method @RequestParam("version") int version, and when calling the delete method pass a query param like ..ajax/products/123?version=1

    As you said request param is not working for you in delete, can you post the exact url you used and the method signature ?

    0 讨论(0)
  • 2020-12-14 17:43

    Passing data in the body of a DELETE request

    @Component public class CustomiseTomcat implements WebServerFactoryCustomizer {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
    
        factory.addConnectorCustomizers( new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                connector.setParseBodyMethods("POST,PUT,DELETE");
            }
        });
    }
    

    }

    0 讨论(0)
  • 2020-12-14 17:49

    The problem is not a Spring problem, but a Tomcat problem.

    By default, Tomcat will only parse arguments that are in the form style, when the HTTP method is POST (at least for version 7.0.54 that I checked but it's probably the same for all Tomcat 7 versions).

    In order to be able to handle DELETE methods as well you need to set the parseBodyMethods attribute of the Tomcat Connector. The connector configuration is done in server.xml.

    Your updated connector would most likely look like:

    <Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000"
               redirectPort="8443"
               parseBodyMethods="POST,PUT,DELETE"
               URIEncoding="UTF-8" />
    

    Here is documentation page for configuring Tomcat connectors.

    Once you setup Tomcat to parse the parameters, Spring will work just fine (although in your case you will probably need to remove @RequestBody from the controller method)

    0 讨论(0)
提交回复
热议问题