Java Spring Boot 2 中缺少 TomcatEmbeddedServletContainerFactory
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47700115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TomcatEmbeddedServletContainerFactory is missing in Spring Boot 2
提问by Anton Krosnev
I have Spring Boot application version 1.5.x, which uses org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
, I'm trying to migrate it to Spring Boot 2, but the app does not compile, although a have a dependency to org.springframework.boot:spring-boot-starter-tomcat
. The compiler issues the error below:
我有春天启动应用程序1.5.x版,它使用org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
,我想将其迁移到春季启动2,但应用程序不编译,虽然有依赖关系org.springframework.boot:spring-boot-starter-tomcat
。编译器发出以下错误:
error: package org.springframework.boot.context.embedded.tomcat
采纳答案by Anton Krosnev
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:
该类已被删除并替换为org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
For more info check: Spring-Boot-2.0-Migration-Guide,其中说:
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.
为了支持响应式用例,嵌入式容器包结构已经进行了相当广泛的重构。EmbeddedServletContainer 已重命名为 WebServer,org.springframework.boot.context.embedded 包已重定位到 org.springframework.boot.web.server。相应地,EmbeddedServletContainerCustomizer 已重命名为 WebServerFactoryCustomizer。
例如,如果您使用 TomcatEmbeddedServletContainerFactory 回调接口自定义嵌入式 Tomcat 容器,您现在应该使用 TomcatServletWebServerFactory,如果您使用的是 EmbeddedServletContainerCustomizer bean,您现在应该使用 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);
}
}
};
}
回答by Rajim
In Spring boot 2.0.0.RELEASEyou can replace with following code::
在 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;
}
回答by womd
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/
太棒了!我来自这篇文章: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:
使用弹簧靴 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;
}
}