Spring WebApplicationInitializer 和 Jetty 8.x

4

我将尝试使用Maven在Jetty上部署一个应用程序:

我已经按照以下配置来设置插件:

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>8.0.4.v20111024</version>
</plugin>

我有一个WebApplicationContextInitializer,为了方便起见,我已经将其简化:

AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();

// Register and map the main dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("appServlet", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(2);
dispatcher.addMapping("/site/*");

当我在Spring Tool Suite中运行jetty:run时,无法访问我的servlet。启动日志如下:

    [INFO] Configuring Jetty for project: Test
    [INFO] webAppSourceDirectory C:\Users\Alex\Documents\spring\Test\src\main\webapp does not exist. Defaulting to C:\Users\Alex\Documents\spring\Test\src\main\webapp
    [INFO] Reload Mechanic: automatic
    [INFO] Classes = C:\Users\Alex\Documents\spring\Test\target\classes
    [INFO] Context path = /
    [INFO] Tmp directory = C:\Users\Alex\Documents\spring\Test\target\tmp
    [INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
    [INFO] Web overrides =  none
    [INFO] web.xml file = null
    [INFO] Webapp directory = C:\Users\Alex\Documents\spring\Test\src\main\webapp
    2012-02-06 21:22:38.048:INFO:oejs.Server:jetty-8.0.4.v20111024
    2012-02-06 21:22:38.807:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
    2012-02-06 21:22:41.828:INFO:/:Spring WebApplicationInitializers detected on classpath: [org.test.application.config.TestWebApplicationInitializer@f946f9]
    2012-02-06 21:22:41.965:INFO:/:Initializing Spring FrameworkServlet 'appServlet'
    INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started
    INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing WebApplicationContext for namespace 'appServlet-servlet': startup date [Mon Feb 06 21:22:41 GMT 2012]; root of context hierarchy
    INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
    INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
    INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18b24cb: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
    INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 235 ms
    2012-02-06 21:22:42.344:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/C:/Users/Alex/Documents/spring/Test/src/main/webapp/},file:/C:/Users/Alex/Documents/spring/Test/src/main/webapp/
    2012-02-06 21:22:42.344:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/C:/Users/Alex/Documents/spring/Test/src/main/webapp/},file:/C:/Users/Alex/Documents/spring/Test/src/main/webapp/
    2012-02-06 21:22:42.345:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/C:/Users/Alex/Documents/spring/Test/src/main/webapp/},file:/C:/Users/Alex/Documents/spring/Test/src/main/webapp/
    2012-02-06 2012-02-06 21:22:42.352:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080 STARTING
    [INFO] Started Jetty Server

如果我访问http://localhost:8080/,会显示默认的Jetty页面,但没有列出上下文。我尝试按照这里所述的将webdefaults.xml复制到我的项目中,但这并没有解决问题,只是删除了默认的servlet页面。在Tomcat中正确部署,因此我怀疑是maven-jetty-plugin的问题。有人对此有经验吗?编辑:我可以确认如果我更改Jetty配置以使应用程序在上下文/application下部署,然后转到http://localhost:8080/application/site/,会收到404错误。然而,调度程序servlet已记录:
No mapping found for HTTP request with URI [/application/site/] in DispatcherServlet with name 'appServlet'

这表明我的控制器映射存在问题,对吗?
启动日志显示已经注册了此映射:
Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.test.application.controller.HomeController.catchAll()

我错过了什么?


1
抱歉问一个愚蠢的问题,但是您已经将映射添加到/site,所以我期望该站点在http://localhost:8080/site/上可用,对吗? - dma_k
我本来也以为是这样,但事实并非如此。 - Alex Barnes
已添加了一些更多的细节。 - Alex Barnes
你为什么要访问 http://localhost:8080/application/site/?你已经将应用程序部署到根上下文中(请参见日志中的 Context path = /),因此URL应该是 http://localhost:8080/site/。你能试试吗?如果这没有帮助,那么请将日志级别增加到 TRACE 级别并在日志中使用 grep 命令。 - dma_k
1
如果您解决了问题,请创建一个包含解决方案的答案,并将其标记为“已接受”。 - Luciano
显示剩余2条评论
1个回答

0

看起来新的Jetty插件将根"/"用作Web应用程序的根上下文,而旧的则如下所述:

contextPath是可选项。您的Web应用程序的上下文路径。默认情况下, 这是从项目的pom.xml中设置的。您可以在此处覆盖它并将其设置为任何您喜欢的内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接