从 web.xml 迁移到基于 Java 的配置 - 无法启动 Tomcat 8
Posted
技术标签:
【中文标题】从 web.xml 迁移到基于 Java 的配置 - 无法启动 Tomcat 8【英文标题】:Migrating from web.xml to Java based configuration - unable to start Tomcat 8 【发布时间】:2015-05-05 03:52:19 【问题描述】:我正在尝试将我的应用程序的 web.xml 迁移到基于 Java 的配置。 我们使用的是 spring 4.1、Java 7、Servlet 3.1、Tomcat 8 和 Eclipse Luna。 Web 服务框架是 Jersey 2.14。
我主要使用以下指南:http://www.robinhowlett.com/blog/2013/02/13/spring-app-migration-from-xml-to-java-based-config/
我创建了遵循 web.xml 配置的 WebApplicationInitializer,删除了 web.xml,将 Maven 配置为不查找 web.xml,并成功执行 mvn clean install。
当我尝试启动 tomcat 时,出现以下错误:
'Publishing to Tomcat v8.o Server at localhost...' has encountered a problem. Resource '/sb-server/target/m2e-wtp/web-resources/WEB-INF/web.xml' does not exist.
我试图清理 tomcat 目录,但它没有帮助,看起来我错过了一些东西,因为 AFAIK Tomcat 8 应该是基于 Java 的配置友好的。
我错过了迁移的步骤吗?
以前的 web.xml(按预期工作):
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0">
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.sb.configuration.ServerConfiguration
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.sb.configuration.RestJaxRsApplication</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.tracing.type</param-name>
<param-value>ALL</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
WebApplicationInitializerImplementation:
@Order(Ordered.HIGHEST_PRECEDENCE)
public class WebAppInitializer implements WebApplicationInitializer
@Override
public void onStartup(final ServletContext container) throws ServletException
// Set up application context
final AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(ServerConfiguration.class);
container.addListener(new ContextLoaderListener(appContext));
// Register listeners
container.addListener(ContextLoaderListener.class);
// Jersey Servlet configuration
final ServletRegistration.Dynamic dispatcher =
container.addServlet("Jersey REST Service", ServletContainer.class);
dispatcher.setInitParameter("javax.ws.rs.Application", "com.sb.configuration.RestJaxRsApplication");
dispatcher.setInitParameter("jersey.config.server.tracing.type", "ALL");
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/api/*");
// Filters
final Dynamic filterRegistration = container.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class);
filterRegistration.addMappingForUrlPatterns(null, false, "/*");
【问题讨论】:
【参考方案1】:有一种更简单的方法。
在你的 pom 文件中添加对 Servlet 3.1 的依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
首先创建一个 Servlet 初始化器。示例如下:
public class WebAppInit implements WebApplicationInitializer
@Override
public void onStartup(ServletContext servletContext) throws ServletException
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.scan(AppConfiguration.class.getPackage().getName());
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", new DispatcherServlet(context));
appServlet.setLoadOnStartup(1);
Set<String> mappings = appServlet.addMapping("/");
if (!mappings.isEmpty())
throw new IllegalStateException("Conflicting mappings found! Terminating. " + mappings);
您的应用程序配置也需要一些设置。示例如下:
@Configuration
@EnableWebMvc
@ComponentScan("com.your.package.root")
public class AppConfiguration extends WebMvcConfigurerAdapter
@Bean
public ViewResolver viewResolver()
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setOrder(0);
return viewResolver;
/**
* Spring and WEB settings from WebConfAdapt.
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
configurer.enable();
除此之外,不需要更多配置。我成功地用这个启动了我的Tomcat。 (但是,这是在 Tomcat 7 上)
【讨论】:
您好,感谢您的回答,但正如我所说,我正在使用 Jersey 而不是 Spring MVC。在我的配置中,您建议的 Maven 依赖项已经存在,WebApplicationConfiguration 实现也存在。我将在问题中添加相关代码。以上是关于从 web.xml 迁移到基于 Java 的配置 - 无法启动 Tomcat 8的主要内容,如果未能解决你的问题,请参考以下文章
无法将 JSF + CDI 项目从 Tomcat 迁移到 Wildfly
Spring Web工程web.xml零配置即使用Java Config + Annotation