In this short article, we will cover some interesting aspects of Spring Boot web application configuration. We will introduce some of the most commonly used configurations for web applications.
1. Introduction
Spring Boot comes with smart build capabilities to easily create web or standalone applications. Spring Boot can do a lot for us, not even needing us to write a line of code for our web applications. In this article, we will only introduce a few of these configurations.
2. HTTP port
One of the most common configurations for web applications is the HTTP port number. We can easily configure HTTP port number for our web applications in the following ways:
2.1 Setting up HTTP ports through configuration
For properties files:
server.port=9001
For YAML files:
server: port: 8083
2.2 Programmatically set the HTTP port number
We can also programmatically set the HTTP port in Spring Boot:
@Componentpublic 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
The default context path for Spring Boot web applications is "/", and Spring Boot provides the option to set the context path by configuring or programmatically.
3.1 Setting the Context path through configuration
For properties files:
server.contextPath=/javadevjournal
For YAML files:
server: contextPath:/javadevjournal
3.2 Programming to set the Context path
We can also programmatically set the Context path in Spring Boot:
@Componentpublic 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 using Spring Boot app, you should be familiar with While Label Error Page. If we do not specify our own custom bean, Spring Boot will automatically register the BasciErrorController bean. We can customize this bean by extending the ErrorController.
@Controllerpublic 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. Customize the error page
Spring Boot provides a way to use our own custom error page based on error codes. We need to add an error code-based page in the /error directory, and Spring Boot will use the correct page according to the error code.
We can use static HTML or templates to build our custom error pages. The name of the file should be the exact status code or series wildcard.
We can use similar structures 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. Configuration log
Spring Boot has no necessary dependency on logging (except for the general logging API). Spring Boot uses LoggingSystem internally to try to configure logs based on the contents of the classpath.
We can use the logging.level prefix in the application.properties file to set the log level so that we can fine-tune the log output of the Spring Boot application.
logging.level.org.springframework.web=DEBUGlogging.level.org.hibernate=ERROR
We can use different logging frameworks (Logback, Log4j2) in Spring Boot application.
Summarize
In this article, we introduce Spring Boot Web Application Configuration, which is necessary to set up your web application correctly or to set it up as you want. For more details, you can always refer to the Spring Boot documentation.
Original link: https://www.javadevjournal.com/spring-boot/spring-boot-web-application-configuration/
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.