16

I created a Servlet (extending from HttpServlet) and annotated as per 3.0 specs with

@WebServlet(name="DelegateServiceExporter", urlPatterns={"/remoting/DelegateService"})

My @Configuration class in Spring Boot scans this servlet's package. However, it does not log that it has deployed this servlet in the embedded Tomcat 8.0.15 container when my Spring Boot application starts up.

So, I added @Component to my servlet as well. Now, Spring Boot registers the servlet (proving to me that the scan package was correctly set up), but then it registers it with a URL pattern based on a class name using camel case. So, that was better - e.g., I got a servlet registered, but with the wrong URL mappings!

2015-01-05 11:29:08,516 INFO  (localhost-startStop-1) [org.springframework.boot.context.embedded.ServletRegistrationBean] Mapping servlet: 'delegateServiceExporterServlet' to [/delegateServiceExporterServlet/]

How do I get Spring Boot to auto-load all @WebServlet annotated servlets and honor their url mappings?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Jason
  • 2,006
  • 3
  • 21
  • 36
  • You don't. Spring Boot uses a different way of configuring and mapping servlets. – M. Deinum Jan 05 '15 at 19:20
  • 1
    I've opened [an issue](https://github.com/spring-projects/spring-boot/issues/2290) to discuss the possibility of honouring `@WebServlet`. – Andy Wilkinson Jan 06 '15 at 09:32
  • 1
    I got it working with the advice here. Thanks everyone! I could not find where Spring Boot documentation says it does not support WebServlet annotation, so that is why I was confused. If I missed the doc, please let me know where it is written. Otherwise, at least an update to the Spring Boot documentation would be helpful - e.g., explicitly say WebServlet annotation is not supported. :) – Jason Jan 06 '15 at 20:19

3 Answers3

46

Add @ServletComponentScan in your bootstrap class.

such as

@SpringBootApplication
@ServletComponentScan 
public class Application {
   public static void main(String[] args) {
       SpringApplication.run(Application.class, args);
   }
}

This will enable spring boot to scan @WebServlet as well as @WebListener.

YMomb
  • 2,366
  • 1
  • 27
  • 36
David Ding
  • 1,473
  • 1
  • 15
  • 13
  • 1
    That should be the accepted answer. You're the best! – ForNeVeR Jan 03 '17 at 14:42
  • (AT)WebServlet is a JEE annotation; thus, it is only picked up when (AT)ServletComponentScan is specified. The latter is an instruction to scan for JEE annotations. – Steve11235 May 08 '18 at 14:01
  • 2
    I recommend specifying (a) Java package(s) to avoid scanning the entire dependency tree; e.g., @ServletComponentScan("org.my.project.servlet") – chrisinmtown Jul 17 '19 at 13:11
7

With Spring Boot, you should use the ServletRegistrationBean object instead of the @WebServlet annotation if you want to register a Servlet and provide the URL pattern.

Adding this bean to your @Configuration class should do the trick :

@Bean
public ServletRegistrationBean delegateServiceExporterServlet() {
    return new ServletRegistrationBean(new DelegateServiceExporter(), "/remoting/DelegateService");
}
Jean-Philippe Bond
  • 10,089
  • 3
  • 34
  • 60
5

It's possible to load servlets annotated with @WebServlet and their mappings in Spring Boot. To do this you need to use @ServletComponentScan with @Configuration annotation. This also works for @WebFilter and @WebListener annotations.

user3007501
  • 283
  • 1
  • 4
  • 13