我创建了一个Servlet (从HttpServlet扩展而来),并按照3.0规范使用
@WebServlet(name="DelegateServiceExporter", urlPatterns={"/remoting/DelegateService"})我的Spring Boot中的@Configuration类扫描这个servlet的包。但是,当我的Spring Boot应用程序启动时,它没有记录它已经在嵌入式Tomcat8.0.15容器中部署了这个servlet。
因此,我也将@Component添加到我的servlet中。现在,Spring Boot注册servlet (向我证明扫描包已正确设置),但随后它使用基于类名的URL模式(使用驼峰大小写)注册它。所以,这样更好--例如,我注册了一个servlet,但是使用了错误的URL映射!
2015-01-05 11:29:08,516 INFO (localhost-startStop-1) [org.springframework.boot.context.embedded.ServletRegistrationBean] Mapping servlet: 'delegateServiceExporterServlet' to [/delegateServiceExporterServlet/]如何让Spring Boot自动加载所有带@WebServlet注释的servlet并遵循它们的url映射?
发布于 2016-09-08 15:58:48
在bootstrap类中添加@ServletComponentScan。
比如
@SpringBootApplication
@ServletComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}这将使spring boot能够像扫描@WebListener一样扫描@WebServlet。
发布于 2015-01-06 05:32:12
在Spring Boot中,如果您想注册Servlet并提供@WebServlet模式,则应该使用ServletRegistrationBean对象而不是@WebServlet注释。
将此bean添加到您的@Configuration类中应该可以做到这一点:
@Bean
public ServletRegistrationBean delegateServiceExporterServlet() {
return new ServletRegistrationBean(new DelegateServiceExporter(), "/remoting/DelegateService");
}发布于 2016-05-27 20:19:22
可以在Spring Boot中加载带有@WebServlet注释的servlet及其映射。要做到这一点,您需要使用带有@Configuration注释的@ServletComponentScan。这也适用于@WebFilter和@WebListener注释。
https://stackoverflow.com/questions/27785935
复制相似问题