Spring boot - Embedded Tomcat - Connector Customizer - fail to add parseBodyMethods attributes
Asked Answered
S

2

3

The original problem is when I sent a http request with method 'DELETE', the body part couldn't be sent to the server.

After googling, I found this article that suggests modifying the server.xml file and adding 'parseBodyMethods' to the Connector part can solve the problem:

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

However, because I'm using spring's embedded tomcat, I have to find a way to do the same in spring's way. So, I found this article that seems to allow me to add ConnectorCustomizer and add additional attribute to the Connector. The following is my code:

    public class MyTomcatConnectorCustomizer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer factory) {
        if(factory instanceof TomcatEmbeddedServletContainerFactory) {
            customizeTomcat((TomcatEmbeddedServletContainerFactory) factory);
        }
    }

    public void customizeTomcat(TomcatEmbeddedServletContainerFactory factory) {
        TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) factory;
        tomcat.addConnectorCustomizers(connector -> {
            connector.setAttribute("parseBodyMethods", "POST,PUT,DELETE");
        });
    }

}

@Bean
MyTomcatConnectorCustomizer myTomcatConnectorCustomizer() {
    MyTomcatConnectorCustomizer myTomcatConnectorCustomizer = new MyTomcatConnectorCustomizer();
    return myTomcatConnectorCustomizer;
}

But still, the same issue exists. the body is still empty when I send a 'DELETE' request to the server. Does anyone have encountered the same issue before? Help appreciated!

Sudoriferous answered 22/10, 2017 at 8:57 Comment(0)
R
1

change

connector.setAttribute("parseBodyMethods", "POST,PUT,DELETE");

to

connector.setParseBodyMethods("POST,PUT,DELETE")

or just

@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
    return new TomcatEmbeddedServletContainerFactory(){
        @Override
        protected void customizeConnector(Connector connector) {
            super.customizeConnector(connector);
            connector.setParseBodyMethods("POST,PUT,DELETE");
        }
    };
}
Retraction answered 15/11, 2017 at 5:42 Comment(0)
I
0

Yes, I know this is a bit old question but I stumbled upon this issue and found that SO don't have a solution for Spring Boot version 2.0.0 and later.

I had found this article on Baeldung which describes the changes in SB 2 versus SB 1.

Quoting the article (code emphasis is mine):

In Spring Boot 2, the EmbeddedServletContainerCustomizer interface is replaced by WebServerFactoryCustomizer, while the ConfigurableEmbeddedServletContainer class is replaced with ConfigurableServletWebServerFactory.

So to fix your issue you need to implement interface WebServerFactoryCustomizer from package org.springframework.boot.web.server and parametrize it with TomcatServletWebServerFactory and override it's customize method.

import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

@Component
public class MyTomcatConnectorCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

  @Override
  public void customize(TomcatServletWebServerFactory factory) {
    TomcatConnectorCustomizer parseBodyMethodCustomizer = connector -> {
      connector.setParseBodyMethods("POST,PUT,DELETE");
    };
    factory.addConnectorCustomizers(parseBodyMethodCustomizer);
  }
}
Intercom answered 18/12, 2019 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.