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

我的未来我决定 提交于 2019-12-08 04:26:49

问题


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!


回答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");
        }
    };
}


来源:https://stackoverflow.com/questions/46872237/spring-boot-embedded-tomcat-connector-customizer-fail-to-add-parsebodymeth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!