Embedded Servlet container (Tomcat) configuration

By default, Spring Boot runs and packages our application using the Tomcat embedded API.

Let's look at EmbeddedServletContainerAutoConfiguration:

@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnWebApplication
@Import(EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar.class)
public class EmbeddedServletContainerAutoConfiguration {

  /**
  * Nested configuration for if Tomcat is being used.
  */
  @Configuration
  @ConditionalOnClass({ Servlet.class, Tomcat.class })
  @ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
  public static class EmbeddedTomcat {

    @Bean
    public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
      return new TomcatEmbeddedServletContainerFactory();
    }

  }

  /**
  * Nested configuration if Jetty is being used.
  */
  @Configuration
  @ConditionalOnClass({ Servlet.class, Server.class, Loader.class })
  @ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
  public static class EmbeddedJetty {

    @Bean
    public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
      return new JettyEmbeddedServletContainerFactory();
    }

  }

  /**
  * Nested configuration if Undertow is being used.
  */
  @Configuration
  @ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })
  @ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
  public static class EmbeddedUndertow {

    @Bean
    public UndertowEmbeddedServletContainerFactory undertowEmbeddedServletContainerFactory() {
      return new UndertowEmbeddedServletContainerFactory();
    }

  }
}

The preceding code is pretty straight forward. This code includes three different configurations, which will be activated depending on what's available on your classpath.

You can use Tomcat, tc-server, Jetty, or Undertow with Spring Boot. Your server can be easily replaced by excluding the spring-boot-starter-tomcat JAR dependency and replacing it with its Jetty or Undertow equivalent. Please refer to the documentation if you wish to do so.

All the configuration of our Servlet container (Tomcat) will happen in TomcatEmbeddedServletContainerFactory. While you should definitely read it because it provides a very advanced configuration of tomcat embedded (for which finding documentation can be hard), we will not look at this class directly.

Instead, I will walk you through the different options available to configure your Servlet Container.

The HTTP port

You can change the default HTTP port by defining a server.port property in your application.properties file or by defining an environment variable called SERVER_PORT.

You can disable HTTP by setting this variable to -1 or launch it on a random port by setting it to 0. This is very handy for testing.

The SSL configuration

Configuring SSL is such a chore, but spring boot has a simple solution. You need only a handful of properties to secure your server:

server.port = 8443
server.ssl.key-store = classpath:keystore.jks
server.ssl.key-store-password = secret
server.ssl.key-password = another-secret

You will have to generate a keystore file for the above example to work, thought.

We'll have a deeper look at our security options in Chapter 6, Securing Your Application. Of course, you can customize the TomcatEmbeddedServletContainerFactory function further by adding your own EmbeddedServletContainerFactory. This can come in handy if you wish to add multiple connectors, for instance. Refer to the documentation at http://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-servlet-containers.html#howto-configure-ssl for more information.

Other configurations

You can add classic Java web elements such as Servlet, Filter, and ServletContextListener by simply declaring them as the @Bean elements in your configuration.

Out of the box, spring boot also added three other things for us:

  • JSON serialization with Jackson in JacksonAutoConfiguration
  • Default HttpMessageConverters in `HttpMessageConvertersAutoConfiguration
  • JMX capabilities in JmxAutoConfiguration

We will see a bit more about the jackson configuration in Chapter 5, Crafting a RESTful Application. About JMX configuration, you can try it out by connecting to your application with jconsole locally:

Other configurations

You can add more interesting MBeans by adding org.springframework.boot:spring-boot-starter-actuator to your classpath. You can even define your own MBeans and expose them on HTTP using Jolokia. On the other hand, you can also disable those endpoints by adding spring.jmx.enabled=false to your configuration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.78.102