Spring Boot 2 中缺少 TomcatEmbeddedServletContainerFactory

新手上路,请多包涵

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

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

原文由 Anton Krosnev 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

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

为了支持反应式用例,嵌入式容器包结构已经进行了相当广泛的重构。 EmbeddedServletContainer 已重命名为 WebServer,org.springframework.boot.context.embedded 包已重新定位到 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);
                }
            }
        };

    }

原文由 Anton Krosnev 发布,翻译遵循 CC BY-SA 4.0 许可协议

在 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;
}

原文由 Rajim 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题