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&
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");
}
};
}
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");
}
};
}
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 ?
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");
}
});
}
}
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)