Configure a Spring Boot Web Application

1. Overview

Spring Boot can do a lot of things; in this tutorial, we’re going to go over a few of the more interesting configuration options in Boot.

2. The Port Number

In main standalone applications, the main HTTP port defaults to 8080; we can easily configure Boot to use a different port:

1
server.port=8083

And for, yaml based configuration:

1
2
server:
     port: 8083

We can also programatically customize the server port:

1
2
3
4
5
6
7
@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {
     @Override
     public void customize(ConfigurableEmbeddedServletContainer container) {
         container.setPort( 8083 );
     }
}

3. The 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:

1
server.contextPath=/springbootapp

And for yaml based configuration:

1
2
server:
     contextPath:/springbootapp

Finally – the change can be done programatically as well:

1
2
3
4
5
6
7
@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {
     @Override
     public void customize(ConfigurableEmbeddedServletContainer container) {
         container.setContextPath( "/springbootapp" );
     }
}

4. The While Label Error Page

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

However, this default controller can of course be configured:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class MyCustomErrorController implements ErrorController {
     private static final String PATH = "/error" ;
     
     @RequestMapping (value=PATH)
     public String error() {
         return "Error heaven" ;
     }
 
     @Override
     public String getErrorPath() {
         return PATH;
     }
}

5. Customize The Error Messages

Boot provides /error mappings by default to handle errors in a sensible way.

If you want to configure more specific error pages, there’s good support for a uniform Java DSL to customize error handling:

1
2
3
4
5
6
7
8
@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {
     @Override
     public void customize(ConfigurableEmbeddedServletContainer container) {       
         container.addErrorPages( new ErrorPage(HttpStatus.BAD_REQUEST, "/400" ));
         container.addErrorPages( new ErrorPage( "/errorHeaven" ));
     }
}

Here, we specifically handled Bad Request to match the /400 path and all others to match the common path.

And a very simple /errorHeaven implementation:

1
2
3
4
@RequestMapping ( "/errorHeaven" )
String errorHeaven() {
     return "You have reached the heaven of errors!!!" ;
}

Output:

1
You have reached the heaven of errors!!!

6. Shut Down A Boot Application Programatically

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

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

It’s through this utility method that we can shut down the app.

7. Configure the Logging Levels

You can easily tune the logging levels in a Boot application; Starting with version 1.2.0 onwards, you can configure the log level in the main properties file:

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

And just as with a standard Spring app – you can activate different logging systems likeLogbacklog4jlog4j2, etc by adding their customized XML or properties file in the classpath and defining the libraries in the pom.

8. 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:

1
2
3
4
@Bean
public HelloWorldServlet helloWorld() {
     return new HelloWorldServlet();
}

Alternatively you can use a ServletRegistrationBean:

1
2
3
4
5
6
7
8
@Bean
public SpringHelloServletRegistrationBean servletRegistrationBean() {
     SpringHelloServletRegistrationBean bean = new SpringHelloServletRegistrationBean(
       new SpringHelloWorldServlet(), "/springHelloWorld/*" );
     bean.setLoadOnStartup( 1 );
     bean.addInitParameter( "message" , "SpringHelloWorldServlet special message" );
     return bean;
}

9. Configure Jetty or Undertow in Boot Application

The Spring Boot starters generally uses Tomcat as the default embedded server. If that needs to be changed – you can exclude the Tomcat dependency and include Jetty or Undertow instead:

Configuring Jetty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< 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 >
1
2
3
4
5
6
7
8
9
@Bean
public JettyEmbeddedServletContainerFactory  jettyEmbeddedServletContainerFactory() {
     JettyEmbeddedServletContainerFactory jettyContainer =
       new JettyEmbeddedServletContainerFactory();
     
     jettyContainer.setPort( 9000 );
     jettyContainer.setContextPath( "/springbootapp" );
     return jettyContainer;
}

Configuring Undertow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< 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 >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
     UndertowEmbeddedServletContainerFactory factory =
       new UndertowEmbeddedServletContainerFactory();
     
     factory.addBuilderCustomizers( new UndertowBuilderCustomizer() {
         @Override
         public void customize(io.undertow.Undertow.Builde builder) {
             builder.addHttpListener( 8080 , "0.0.0.0" );
         }
     });
     
     return factory;
}

10. Conclusion

In this quick article, we went over some of the more interesting and useful Spring Boot configuration options.

There are of course many, many more options to configure and tune a Boot app to your needs in the reference docs – these are just some of the more useful I found.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值