Spring Boot does not honor @WebServlet

☆樱花仙子☆ 提交于 2019-12-04 01:41:23
David Ding

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.

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");
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!