Configuring Spring Boot Web App

Spread the love

Spring boot has a plethora of tools and configuration available and can be tweaked according to the project specific needs. We’are going to go over a few interesting configuration options in Spring Boot.

1. Port Number

In the main Standalone applications, the default port number is always 8080. We can easily configure the port number in Spring Boot.

Change the port as below in your Application.porperties file.

server.port=8088

For YAML based configuration, change as below in your application.yml file

server:
    port: 8088

We can also programmatically customize the server port:

@Component
public class CustomConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        factory.setPort(8088);
    }
}

You can choose any of the methods for port configuration.

2. Context Path

By default, the context path is “/”. If that’s not ideal and you need to change it – to something like /app_name, here’s the quick and simple way to do it via properties:

server.servlet.contextPath=/springbootwebapp

For YAML-based configuration:

server:
    servlet:
        contextPath:/springbootwebapp

We can also programmatically customize the Context Path

@Component
public class CustomConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        factory.setPort(8088);
        factory.setContextPath("/springbootwebapp");
    }
}

3. White Label Error Page

Spring Boot automatically registers a BasicErrorController bean if you don’t specify any custom implementation in the configuration.

However, we can configure this default controller.

public class CustomErrorController implements ErrorController {
  
    private static final String ERROR_PATH = "/error";
     
    @GetMapping(value=ERROR_PATH)
    public String error() {
        return "Error Custom";
    }
 
    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }
}

4. Shut Down a Spring Boot Application

You can programmatically shut down a Boot app with the help of SpringApplication. This has a static exit() method that takes two arguments: the ApplicationContext and an ExitCodeGenerator:

@Autowired
public void shutDown(ExecutorServiceExitCodeGenerator exitCodeGenerator) {
    SpringApplication.exit(applicationContext, exitCodeGenerator);
}

5. Configure the Logging Levels

Configuring the logging levels are very easy in Spring Boot Application. We have to configure the logging properties in the main property file as we are configuring the other properties.

You can select any logging framework in Spring Boot application like Logbacklog4jlog4j2, etc. You just have to define the appropriate dependency in the POM file

logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR

6. Register a New Servlet

If you’re deploying the application with the help of the embedded server, you can register new Servlets in a Boot application by exposing them as beans from conventional config:

@Bean
public HelloWorldServlet helloWorld() {
    return new HelloWorldServlet();
}

Alternatively, you can use a ServletRegistrationBean:

@Bean
public SpringHelloServletRegistrationBean servletRegistrationBean() {
  
    SpringHelloServletRegistrationBean bean = new SpringHelloServletRegistrationBean(
      new SpringHelloWorldServlet(), "/springHelloWorld/*");
    bean.setLoadOnStartup(1);
    bean.addInitParameter("message", "SpringHelloWorldServlet special message");
    return bean;
}

7. Configure Server in Spring Boot Application

By default, Spring boot starters use Tomcat as the default embedded server. IF that needs to be changed – you have to exclude the Tomcat server dependency and add different server (Jetty, Undertow etc) instead.

Configuring Jetty

<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>
@Bean
public JettyEmbeddedServletContainerFactory  jettyEmbeddedServletContainerFactory() {
    JettyEmbeddedServletContainerFactory jettyContainer = 
      new JettyEmbeddedServletContainerFactory();
     
    jettyContainer.setPort(8088);
    jettyContainer.setContextPath("/springbootwebapp");
    return jettyContainer;
}

Configuring Undertow

<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-undertow</artifactId>
</dependency>
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
    UndertowEmbeddedServletContainerFactory factory = 
      new UndertowEmbeddedServletContainerFactory();
     
    factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
        @Override
        public void customize(io.undertow.Undertow.Builder builder) {
            builder.addHttpListener(8088, "0.0.0.0");
        }
    });
     
    return factory;
}

8. Conclusion

In this tutorial, we went over some interesting Spring Boot Configuration options. There are a lot more which can be configured. Please see the Spring Boot Documentation.


Check out more Spring Boot tutorial.