Spring Boot 2中缺少TomcatEmbeddedServletContainerFactory

Ant*_*nev 53 java spring spring-boot

我有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
Run Code Online (Sandbox Code Playgroud)

小智 62

在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;
}
Run Code Online (Sandbox Code Playgroud)

  • 我得到`IllegalStateException:没有ServletContext设置`如果我这样做 - 任何想法我缺少什么? (4认同)
  • 这应该是公认的答案.正是我在寻找什么.我做的唯一不同的是这一行:`连接器连接器=新连接器(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);`谢谢你的回答. (2认同)

Ant*_*nev 43

该类已被删除并替换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);
                }
            }
        };

    }
Run Code Online (Sandbox Code Playgroud)


wom*_*omd 6

太棒了,谢谢!我来自这篇文章: 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;
  }

}
Run Code Online (Sandbox Code Playgroud)