Spring Boot 2中缺少TomcatEmbeddedServletContainerFactory

时间:2017-12-07 17:00:38

标签: java spring spring-boot

我有Spring Boot应用程序版本1.5.x,它使用org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory,我试图将其迁移到Spring Boot 2,但是应用程序没有编译,尽管它依赖{{1} 1}}。编译器发出以下错误:

org.springframework.boot:spring-boot-starter-tomcat

3 个答案:

答案 0 :(得分:48)

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

答案 1 :(得分:35)

该类已被删除并替换为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。

     

例如,如果您要自定义嵌入式Tomcat容器   使用TomcatEmbeddedServletContainerFactory回调接口,   你现在应该使用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);
                }
            }
        };

    }

答案 2 :(得分:0)

太棒了! 我来自这篇文章: https://blog.swdev.ed.ac.uk/2015/06/24/adding-embedded-tomcat-ajp-support-to-a-spring-boot-application/

使用Spring Boot 2.1.3:

@Configuration
@Data
public class TomcatConfiguration {

@Value("${tomcat.ajp.port}")
int ajpPort;

@Value("${tomcat.ajp.remoteauthentication}")
String remoteAuthentication;

@Value("${tomcat.ajp.enabled}")
boolean tomcatAjpEnabled;

@Bean
public TomcatServletWebServerFactory servletContainer() {

    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    if (tomcatAjpEnabled)
    {
        Connector ajpConnector = new Connector("AJP/1.3");
        ajpConnector.setPort(ajpPort);
        ajpConnector.setSecure(false);
        ajpConnector.setAllowTrace(false);
        ajpConnector.setScheme("https");
        tomcat.addAdditionalTomcatConnectors(ajpConnector);
    }

    return tomcat;
  }

}