Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TomcatEmbeddedServletContainerFactory is missing in Spring Boot 2

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:

error: package org.springframework.boot.context.embedded.tomcat 
like image 606
Anton Krosnev Avatar asked Dec 07 '17 17:12

Anton Krosnev


People also ask

What is TomcatEmbeddedServletContainerFactory?

public class TomcatEmbeddedServletContainerFactory extends AbstractEmbeddedServletContainerFactory implements ResourceLoaderAware. EmbeddedServletContainerFactory that can be used to create TomcatEmbeddedServletContainer s. Can be initialized using Spring's ServletContextInitializer s or Tomcat LifecycleListener s.

What is EmbeddedServletContainerCustomizer?

public interface EmbeddedServletContainerCustomizer. Strategy interface for customizing auto-configured embedded servlet containers. Any beans of this type will get a callback with the container factory before the container itself is started, so you can set the port, address, error pages etc.

What is TomcatServletWebServerFactory?

public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory implements ConfigurableTomcatWebServerFactory, ResourceLoaderAware. AbstractServletWebServerFactory that can be used to create TomcatWebServer s.


2 Answers

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; } 
like image 181
Rajim Avatar answered Sep 28 '22 06:09

Rajim


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);                 }             }         };      } 
like image 27
Anton Krosnev Avatar answered Sep 28 '22 06:09

Anton Krosnev