【发布时间】:2012-02-28 08:48:53
【问题描述】:
我正在尝试使用 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 页面,其中没有列出任何上下文。
我已尝试按照here 的描述将 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()
我错过了什么?
【问题讨论】:
-
抱歉这个愚蠢的问题,但您已将映射添加到
/site,所以我希望该网站可以在http://localhost:8080/site/上使用,不是吗? -
我也有同样的期望。但事实并非如此。
-
添加了更多细节。
-
你为什么要去
http://localhost:8080/application/site/?您已将应用程序部署到根上下文(请参阅日志中的Context path = /),因此 URL 应为http://localhost:8080/site/。你能试试吗?如果没有帮助,则将日志记录提高到 TRACE 级别并 grep 日志。 -
如果您解决了问题,请使用解决方案创建答案并将其标记为已接受
标签: spring maven spring-mvc jetty