Category Archives: Configuration

Spring Boot applications supply a variety of powerful mechanisms for developers to dynamically configure and reconfigure their applications, even while the app is running. In this section we will learn how to configure Spring Boot components (such as the Web server and other components) to achieve your desired configuration.

Configuring HTTP Session timeout in Spring Boot applications

You can configure HTTP Session Timeout for Spring Boot Applications in two ways:

Configuring Session Timeout in application.properties

The simplest option is to include in your application.properties the parameter server.servlet.session.timeout. For example:

server.servlet.session.timeout=60s

Also note that Tomcat will not allow you to set the timeout any less than 60 seconds.

Configuring Session Timeout Programmatically

Let’s say we want that our HttpSession last only two minutes. To make this happen, we can add to our WebConfiguration class an EmbeddedServletContainerCustomizer Bean with the following content:

Continue reading

Configure Spring Boot to use Jetty Server

By default, Spring boot uses Tomcat as embedded Web server. There are, however, other available Web servers in case you need some specific features. In this tutorial we will learn how to use Jetty as Web Server.

Add spring-boot-starter-jetty dependency

You will need to update pom.xml and add dependency for spring-boot-starter-jetty. Also, you will need to exclude default added spring-boot-starter-tomcat dependency as follows:

<?xml version="1.0" encoding="UTF-8"?><project>
   <dependency>
           
      <groupId>org.springframework.boot</groupId>
           
      <artifactId>spring-boot-starter-web</artifactId>
           
      <exclusions>
                  
         <exclusion>
                         
            <groupId>org.springframework.boot</groupId>
                         
            <artifactId>spring-boot-starter-tomcat</artifactId>
                     
         </exclusion>
              
      </exclusions>
       
   </dependency>
    
   <dependency>
           
      <groupId>org.springframework.boot</groupId>
           
      <artifactId>spring-boot-starter-jetty</artifactId>
       
   </dependency>
    
</project>
 

If you are using Gradle as build tool, the same result can be achieved using:

configurations {
    compile.exclude module: "spring-boot-starter-tomcat"
}
 
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:2.1.0.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-jetty:2.1.0.RELEASE")
}

Configuring Jetty

The Web server can be configured by overriding the default configuration through the application.properties file.

application.properties
server.port=8080
server.servlet.context-path=/home
 
####Jetty specific properties########
 
server.jetty.acceptors= # Number of acceptor threads to use.
server.jetty.max-http-post-size=0 # Maximum size in bytes of the HTTP post or put content.
server.jetty.selectors= # Number of selector threads to use.

Also, you may configure these options programatically using ConfigurableServletWebServerFactory and JettyServletWebServerFactory classes:

  @Bean
  public ConfigurableServletWebServerFactory webServerFactory() {
    JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
    factory.setPort(9000);
    factory.setContextPath("/myapp");
    factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
    return factory;
  }