java - Spring JavaConfig : Add mapping for custom Servlet

标签 java spring spring-boot h2

在基于 javaconfig 的 Spring 4.0 项目中,如何将某个 URL 的映射添加到 Spring DispatcherServlet 以外的 Servlet。

在我的例子中,我想使用通过 servlet org.h2.server.web.WebServlet

提供的 H2 数据库中的 h2console

编辑: 在即将推出的 Spring Boot 1.3 中,可以使用配置参数启用 h2console:http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-sql-h2-console

启用它就像将这两行添加到您的 application.properties 一样简单:

spring.h2.console.enabled=true
spring.h2.console.path=/console

最佳答案

最简单的方法是使用初始化器直接实现 WebApplicationInitializer 并添加到 onStartup(ServletContext servletContext) 方法后面的代码中;

ServletRegistration.Dynamic h2Servlet = servletContext.addServlet("h2Servlet", new org.h2.server.web.WebServlet());
h2Servlet.setLoadOnStartup(1);
h2Servlet.addMapping("/h2/*");

关于java - Spring JavaConfig : Add mapping for custom Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21244066/

相关文章:

spring - 如何使用 Spring Boot 自动重定向到 https

java - 如何在同一台机器上运行多个 selenium 测试套件

Java:具有两个键的 HashMap

java - 多个作业中的 Spring Batch 相同的步骤名称?

java - 将过滤器 bean 添加到 ServletContext

java - 使用倒计时闩锁或 Spring 集成并行调用 Web 服务哪个更好?

java - 堆栈跟踪警告 "No bean factory"

java - 即使包含 User-Agent 属性,从 java 对网站的 GET 请求也会返回 403

java - 我的问题是如何根据 PDF 字段名称放置黄色选项卡

java - 模板中的 Spring IOC 嵌套 bean 覆盖属性值