Spring Boot Web Application Configuration

Spring Boot Web Application Configuration

In this short post, we will cover some of the interesting aspects of the Spring Boot web application configuration. We will be covering some of the most commonly used configurations for a web application.

 

1. Introduction

Spring Boot comes with build in intelligence which makes it easy to create the web or standalone application.Spring Boot can do a lot of things for us without even writing a single line of code for our web application and we will be covering few of those configurations.

 

2. HTTP Port

One of the common configurations for a web application is the HTTP port number, we can easily configure HTTP port number for our application using following options

  • Using application.properties file
  • By YAML based configuration
  • Setting HTTP port number programmatically.

 

2.1 Setting HTTP Port by Configuration

For properties file

server.port=9001

For YAML

server:
        port: 8083

 

2.2 Setting HTTP Port by Programmatically

We can also set HTTP port programmatically in Spring Boot

@Component
public class CustomConfiguration implements EmbeddedServletContainerCustomizer {
    /**
     * Customize the specified {@link ConfigurableEmbeddedServletContainer}.
     *
     * @param container the container to customize
     */
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setPort(9001);
    }
}

 

3. Context Path

Default context for a Spring Boot web application is "/", Spring Boot provides the option to set context either through configuration or programmatically.

 

3.1 Setting Context by Configuration

For properties file

server.contextPath=/javadevjournal

For YAML

server:
        contextPath:/javadevjournal

 

3.2 Setting Context by Programmatically

We can also set context programmatically in Spring Boot

@Component
public class CustomConfiguration implements EmbeddedServletContainerCustomizer {
    /**
     * Customize the specified {@link ConfigurableEmbeddedServletContainer}.
     *
     * @param container the container to customize
     */
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setPort(9001);
        container.setContextPath("/javadevjournal");
    }
}

 

4. BasicErrorController

If you are working with Spring Boot application, you should be familiar with While Label Error Page. Spring Boot automatically register BasciErrorController bean if we do not specify our own custom bean. We can customize this by extending ErrorController.

@Controller
public class CustomErrorController implements ErrorController {

    private static final String PATH = "/error";


    @RequestMapping(value = PATH)
    public String error() {
        return "errorHandling";
    }

    /**
     * Returns the path of the error page.
     *
     * @return the error path
     */
    @Override
    public String getErrorPath() {
        return PATH;
    }
}

5. Custom Error Pages

Spring Boot provides a way to use your own custom error pages based on the error code. We need to add error code based pages under the /error directory and Spring Boot will use correct page based on the error code.

We can use either static HTML or can use a template to build our custom error pages. The name of the file should be the exact status code or a series mask.

We can use a similar structure to organize our templates.

src/
 +- main/
          +- java/
          |      + <source code>
          +- resources/
                  +- public/
                          +- error/
                           |     +- 404.html
                           +- <other public assets>

 

src/
 +- main/
          +- java/
          |      + <source code>
          +- resources/
                  +- public/
                          +- error/
                           |     +- 5xx.html
                           +- <other public assets>

 

6. Configure Logging

Spring Boot has no required dependencies for the logging (except common logging API). Spring Boot internally use LoggingSystem abstraction that attempts to configure logging based on the content of the classpath.

We can fine-tune log out in Spring Boot application by setting log level in the application.properties file using logging. level as a predefined prefix

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

 

We can use different logging framework (Logback, Log4j2) in Spring Boot application.

 

Summary

In this post, we covered  Spring Boot Web application configuration which is required for setting up your web application correctly or as per your need. For more details, you can always refer to Spring Boot documentation.

Comments are closed.