$show=/label

Custom Container Configuration in Spring Boot 2

SHARE:

A quick guide to use EmbeddedServletContainerCustomizer and ConfigurableEmbeddedServletContainer in Spring Boot 2. These two are replaced in spring boot 2.0 with WebServerFactoryCustomizer interface and ConfigurableServletWebServerFactory class.

1. Introduction


In this tutorial, We'll learn how to use EmbeddedServletContainerCustomizer and ConfigurableEmbeddedServletContainer in Spring Boot 2. But, these two are not present in the newer version of spring boot 2. These two are part of earlier versions 1.x. But, an alternative for the same functionality is provided with WebServerFactoryCustomizer interface and ConfigurableServletWebServerFactory class.

All these interfaces are mainly used to set the configurations for the container such as setting up the port, adding basepath or context path and also these can be set to the specific embed servers such as tomcat or jetty or undertow.

Custom Container Configuration in Spring Boot 2


Differences between spring and spring boot

2. Spring Boot 1.x Versions


EmbeddedServletContainerCustomizer Example


This is a Strategy interface and it is 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.

This interface has customize() method which takes as ConfigurableEmbeddedServletContainer class argument.

On ConfigurableEmbeddedServletContainer class, we can call setPort(), setContextpath() and addErrorPages() methods. All these changes are applicable to all embed servers what ever you are using.


@Component
public class CustomEmbedServerConfigrationForAllServers1_x implements EmbeddedServletContainerCustomizer {
  
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
 
        container.setPort(8080);
        container.setContextPath("");
        container.setSessionTimeout(60); // in seconds
  
  ErrorPage errorPage = new ErrorPage(HttpStatus.NOT_FOUND, "/var/error_404.jsp");
  
  container.addErrorPages(errorPage); // adding error page.
  
     }
}

In the above code, we've set the port, base path and timeout time in seconds along with the error page.

Tomcat Embedded Server Specific:


There is a possibility to set container configuration changes to the Tomcat Embed server alone by using TomcatEmbeddedServletContainerFactory which is a subclass of ConfigurableEmbeddedServletContainer.

@Component
public class TomcatCustomContainer implements EmbeddedServletContainerCustomizer {
  
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        if (container instanceof TomcatEmbeddedServletContainerFactory) {
            TomcatEmbeddedServletContainerFactory tomcatContainer = (TomcatEmbeddedServletContainerFactory) container;
            tomcatContainer.setPort(8080);
            tomcatContainer.setContextPath("");
   
            tomcatContainer.setSessionTimeout(60); // in seconds
   
   ErrorPage errorPage = new ErrorPage(HttpStatus.BAD_REQUEST, "/var/error_400.jsp");
   container.addErrorPages(errorPage); // adding error page.
   
        }
    }
}

The same can be done for Jetty and UnderTow servers by using JettyEmbeddedServletContainerFactory and UndertowEmbeddedServletContainerFactory implementations.

3. Spring Boot 2.x Versions (Upgrade)


In Spring Boot 2, the EmbeddedServletContainerCustomizer interface is replaced by WebServerFactoryCustomizer, while the ConfigurableEmbeddedServletContainer class is replaced with ConfigurableServletWebServerFactory.

package com.javaprogram.config;

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class EmbedServerCustomConfigration implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

 @Override
 public void customize(ConfigurableServletWebServerFactory factory) {

  factory.setPort(9009);
  factory.setDisplayName("JavaProgramTo.com");
  factory.setServerHeader("Server header");

  factory.setContextPath("/api/v3");

 }

}

If you want to disable this custom configuration then just comment @Component annotation from the above class.

Tomcat Related Configurations:


package com.javaprogram.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

@Component
public class TomcatEmbedServerCustomConfigration implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

 private Logger logger = LoggerFactory.getLogger(getClass());

 @Override
 public void customize(TomcatServletWebServerFactory factory) {
  logger.info("Setting the Tomcat specific configurations. started");
  factory.setPort(9009);
  factory.setDisplayName("JavaProgramTo.com");
  factory.setServerHeader("Server header of tomcat");

  factory.setContextPath("/api/v4");
  logger.info("Setting the Tomcat specific configurations. ended");
 }

}

Output:

2020-04-18 12:13:09.679  INFO 58221 --- [           main] c.j.s.SpringBootAppApplication           : No active profile set, falling back to default profiles: default
2020-04-18 12:13:11.107  INFO 58221 --- [           main] .j.c.TomcatEmbedServerCustomConfigration : Setting the Tomcat specific configurations. started
2020-04-18 12:13:11.107  INFO 58221 --- [           main] .j.c.TomcatEmbedServerCustomConfigration : Setting the Tomcat specific configurations. ended
2020-04-18 12:13:11.294  INFO 58221 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9009 (http)

Endpont should be : localhost:9009/api/v4/headers/individual/default
here version chaned to v4.

Similarly, we have the JettyServletWebServerFactory and UndertowServletWebServerFactory as equivalents for the removed JettyEmbeddedServletContainerFactory and UndertowEmbeddedServletContainerFactory.

5. Exceptions


If the server context path does not start with "/" then will get the below error. Need to change to "/api/v3" instead of "api/v3". This is the exception that I faced during developing of this use case.

End point: localhost:9009/api/v3/headers/individual/default

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatServletWebServerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at com.javaprogram.springbootapp.SpringBootAppApplication.main(SpringBootAppApplication.java:16) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatServletWebServerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:603) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:210) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 ... 8 common frames omitted
Caused by: java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
 at org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory.checkContextPath(AbstractServletWebServerFactory.java:133) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory.setContextPath(AbstractServletWebServerFactory.java:122) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at com.javaprogram.config.EmbedServerCustomConfigration.customize(EmbedServerCustomConfigration.java:17) ~[classes/:na]
 at com.javaprogram.config.EmbedServerCustomConfigration.customize(EmbedServerCustomConfigration.java:1) ~[classes/:na]
 at org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor.lambda$postProcessBeforeInitialization$0(WebServerFactoryCustomizerBeanPostProcessor.java:72) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.util.LambdaSafe$Callbacks.lambda$null$0(LambdaSafe.java:287) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.util.LambdaSafe$LambdaSafeCallback.invoke(LambdaSafe.java:159) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.util.LambdaSafe$Callbacks.lambda$invoke$1(LambdaSafe.java:286) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_161]
 at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1080) ~[na:1.8.0_161]
 at org.springframework.boot.util.LambdaSafe$Callbacks.invoke(LambdaSafe.java:286) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor.postProcessBeforeInitialization(WebServerFactoryCustomizerBeanPostProcessor.java:72) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor.postProcessBeforeInitialization(WebServerFactoryCustomizerBeanPostProcessor.java:58) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 ... 16 common frames omitted

5. Conclusion


In this article, We've seen how to replace EmbeddedServletContainerCustomizer and ConfigurableEmbeddedServletContainer in Spring Boot 2. Example programs on setting custom configurations in 1.x and 2.x versions.

And also seen the common errors that come as part of the development and are specific to embed servers for Tomcat, Jetty and Undertow servers.

GitHub

Tomcat Specific code

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Custom Container Configuration in Spring Boot 2
Custom Container Configuration in Spring Boot 2
A quick guide to use EmbeddedServletContainerCustomizer and ConfigurableEmbeddedServletContainer in Spring Boot 2. These two are replaced in spring boot 2.0 with WebServerFactoryCustomizer interface and ConfigurableServletWebServerFactory class.
https://blogger.googleusercontent.com/img/a/AVvXsEioGSOrpYd8ZH5IvQL0HSeZjOQrs1nVNs44B15hchIKL0kaybfyRWkyvgIDRSBdfL2cl7vG4TqjOk1cX8XCfqx2QsPWQ1G5lf9rV81eUuD-PorHez5_v8-M4seE1yb-bZG8flj4LdXF02z4UMTF5750AUkeGbI_xBYZVRYZHb01pXyAW7vwhfUJ7hgj=w400-h223
https://blogger.googleusercontent.com/img/a/AVvXsEioGSOrpYd8ZH5IvQL0HSeZjOQrs1nVNs44B15hchIKL0kaybfyRWkyvgIDRSBdfL2cl7vG4TqjOk1cX8XCfqx2QsPWQ1G5lf9rV81eUuD-PorHez5_v8-M4seE1yb-bZG8flj4LdXF02z4UMTF5750AUkeGbI_xBYZVRYZHb01pXyAW7vwhfUJ7hgj=s72-w400-c-h223
JavaProgramTo.com
https://www.javaprogramto.com/2020/04/spring-boot-embeddedservletcontainercustomizer-configurableembeddedservletcontainer.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/04/spring-boot-embeddedservletcontainercustomizer-configurableembeddedservletcontainer.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content