Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot does not honor @WebServlet

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?

like image 955
Jason Avatar asked Jan 05 '15 18:01

Jason


1 Answers

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.

like image 164
David Ding Avatar answered Sep 23 '22 18:09

David Ding