TomcatEmbeddedServletContainerFactory 在 Spring Boot 2 中缺失

2022-08-31 13:09:32

我有Spring Boot应用程序版本1.5.x,它使用,我正在尝试将其迁移到Spring Boot 2,但该应用程序无法编译,尽管与.编译器发出以下错误:org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactoryorg.springframework.boot:spring-boot-starter-tomcat

error: package org.springframework.boot.context.embedded.tomcat

答案 1

在Spring boot 2.0.0.RELEASE中,您可以替换为以下代码:

@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(redirectConnector());
    return tomcat;
}

private Connector redirectConnector() {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setScheme("http");
    connector.setPort(8080);
    connector.setSecure(false);
    connector.setRedirectPort(8443);
    return connector;
}

答案 2

该类已被删除并替换为 有关更多信息,请检查:Spring-Boot-2.0-Migration-Guide,其中说:org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory

为了支持反应式用例,嵌入式容器包结构已经进行了相当广泛的重构。EmbeddedServletContainer已重命名为WebServer,org.springframework.boot.context.embedded package已重新定位到org.springframework.boot.web.server。相应地,EmbeddedServletContainerCustomizer已更名为WebServerFactoryCustomizer。

例如,如果您使用 TomcatEmbeddedServletContainerFactory 回调接口自定义嵌入式 Tomcat 容器,则现在应该使用 TomcatServletWebServerFactory,如果您使用的是 EmbeddedServletContainerCustomizer bean,则现在应该使用 WebServerFactoryCustomizer Bean。

我遇到了一个问题,我需要发送更大的请求,然后允许默认大小:

@Bean
    public TomcatServletWebServerFactory containerFactory() {
        return new TomcatServletWebServerFactory() {
            protected void customizeConnector(Connector connector) {
                int maxSize = 50000000;
                super.customizeConnector(connector);
                connector.setMaxPostSize(maxSize);
                connector.setMaxSavePostSize(maxSize);
                if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {

                    ((AbstractHttp11Protocol <?>) connector.getProtocolHandler()).setMaxSwallowSize(maxSize);
                    logger.info("Set MaxSwallowSize "+ maxSize);
                }
            }
        };

    }

推荐