在 SpringBoot2中缺少 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
61601 次浏览

The class has been removed and replaced by org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory For more info check: Spring-Boot-2.0-Migration-Guide, which says:

In order to support reactive use cases, the embedded containers package structure has been refactored quite extensively. EmbeddedServletContainer has been renamed to WebServer and the org.springframework.boot.context.embedded package has been relocated to org.springframework.boot.web.server. Correspondingly, EmbeddedServletContainerCustomizer has been renamed to WebServerFactoryCustomizer.

For example, if you were customizing the embedded Tomcat container using the TomcatEmbeddedServletContainerFactory callback interface, you should now use TomcatServletWebServerFactory and if you were using an EmbeddedServletContainerCustomizer bean, you should now use a WebServerFactoryCustomizer bean.

I had the problem that I needed to sent bigger request, then the default size allowed:

@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);
}
}
};


}

In Spring boot 2.0.0.RELEASE you can replace with following code::

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

Great Thx! I came from this article: https://blog.swdev.ed.ac.uk/2015/06/24/adding-embedded-tomcat-ajp-support-to-a-spring-boot-application/

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


}