Spring Boot does not honor @WebServlet

前端 未结 3 1279
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-13 12:38

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

@WebServlet(name=\"DelegateServiceExporter\", urlPatterns={\"/remoting/Deleg         


        
相关标签:
3条回答
  • 2021-02-13 13:18

    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.

    0 讨论(0)
  • 2021-02-13 13:24

    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.

    0 讨论(0)
  • 2021-02-13 13:25

    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");
    }
    
    0 讨论(0)
提交回复
热议问题