0

We are moving from Spring WebInitializer to Spring boot 2.5.0 SpringBootServletInitializer

We need to add a context path for our Servlet dispatcher, with the following code:

@SpringBootApplication(exclude = { JmsAutoConfiguration.class, ActiveMQAutoConfiguration.class,
            SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class })
    public class WebServerConfig extends SpringBootServletInitializer implements ApplicationContextAware {
    @Bean
   public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
       return factory -> {
           factory.setRegisterDefaultServlet(true);
           factory.setContextPath("/servlet");
       };
   }
}

When running Maven WAR regular Tomcat 8.5, the Servlet context isn't set, log:

o.a.c.c.C.[Catalina].[localhost].[/]     : Initializing Spring DispatcherServlet 'dispatcherServlet'

Regular Main

public static void main(String[] args) {
    SpringApplication.run(WebServerConfig.class, args);
}

When running embedded Tomcat code works as expected:

o.a.c.c.C.[.[localhost].[/servlet]       : Initializing Spring DispatcherServlet 'dispatcherServlet'

Maven pom:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>${springframework.boot-version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>${springframework.boot-version}</version>
    <scope>provided</scope>
</dependency>

I also have configure method:

@Override
protected SpringApplicationBuilder configure(
  SpringApplicationBuilder builder) {
    return builder.sources(WebServerConfig.class);
}

Am I missing code/property to enable Servlet context when building a WAR?

I found older question and question2 without a real answer except using different war name.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • You need to override/implement the `configure` method of the `SpringBootServletInitializer` else it won't do anything. – M. Deinum Jun 01 '21 at 05:53
  • @M.Deinum I added also `configure` method to my question – Ori Marko Jun 01 '21 at 05:55
  • From those snippets it is impossible to tell, and your config also looks weird. Also depending on your Spring Boot version Tomcat 8.5 might be too low and you require tomcat 9. – M. Deinum Jun 01 '21 at 05:56
  • And ofcourse the `WebServerFactoryCustomizer` doesn't apply when deploying as a war file. That is for controlling the creation of the embedded tomcat, spring Boot cannot control an external tomcat. So the context path (which is something different than a servletcontext (which threw me off) will not be set). – M. Deinum Jun 01 '21 at 05:58
  • @M.Deinum with tomcat 9 same results, so what is the equivalent to Spring `ServletRegistration.Dynamic dispatcher = servletContext.addServlet("Mydispatcher", ds); dispatcher.addMapping("/servlet/*");` – Ori Marko Jun 01 '21 at 06:10
  • Ofcourse you will get the same results, as I mentioned that thing doesn't apply. Remove that line and add `spring.mvc.servlet.path=/servlet/*` to your `application.properties` (at least that is what your code snippet of the servlet registration implies which again is different than the context path, so which is it you want to set?). – M. Deinum Jun 01 '21 at 06:25

2 Answers2

1

Use the below application property to set the context path for spring boot application. If you are using yaml configuration.

server:
   servlet:
     context-path: /servlet

If you are using application.properties

server.servlet.context-path = /servlet
kiranNswamy
  • 126
  • 9
1

We fixed the issue by adding property inside SpringApplicationBuilder:

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    Properties bootProperties = new Properties();
    bootProperties.setProperty("spring.mvc.servlet.path", "/servlet");
    return builder.sources(WebServerConfig.class).properties(bootProperties);
}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233