about spring boot how to disable web environment correctly

Starting from Spring Boot 2.0

-web(false)/setWebEnvironment(false) is deprecated and instead Web-Application-Type can be used to specify

  • Application Properties

    spring.main.web-application-type=NONE 
    # REACTIVE, SERVLET
    
  • or SpringApplicationBuilder

    @SpringBootApplication
    public class SpringBootDisableWebEnvironmentApplication {
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(SpringBootDisableWebEnvironmentApplication.class)
                .web(WebApplicationType.NONE) // .REACTIVE, .SERVLET
                .run(args);
       }
    }
    

Where WebApplicationType:

  • NONE – The application should not run as a web application and should not start an embedded web server.
  • REACTIVE – The application should run as a reactive web application and should start an embedded reactive web server.
  • SERVLET – The application should run as a servlet-based web application and should start an embedded servlet web server.

Courtesy: Another SO Answer

Leave a Comment