Pages

Wednesday, December 22, 2021

Custom Container Configuration in Spring Boot 2

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

No comments:

Post a Comment

Please do not add any spam links in the comments section.