Spring Boot 2中缺少TomcatEmbeddedServletContainerFactory

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

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

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

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