Spring WebApplicationInitializer 和 Jetty 8.x

标签 spring maven spring-mvc jetty

我正在尝试使用 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 复制到我的项目中,如所述 here但这并没有解决问题。它只是删除了默认的 servlet 页面。

这可以在 Tomcat 中正确部署,因此我怀疑 maven-jetty-plugin 中存在问题。

有人有这方面的经验吗?

编辑:

所以我可以确认,如果我更改 Jetty 配置,以便将应用程序部署在 context/application 下,然后导航到 http://localhost:8080/application/site/我得到了 404。

但是,调度程序 servlet 已记录:

No mapping found for HTTP request with URI [/application/site/] in DispatcherServlet with name 'appServlet'

这表明我的 Controller 映射存在问题,是否正确?

启动日志显示此映射已注册:

Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.test.application.controller.HomeController.catchAll()

我错过了什么?

最佳答案

新的 Jetty 插件似乎使用根“/”作为 Web 应用程序的根上下文,旧的插件如下所示:

contextPath Optional. The context path for your webapp. By default, this is set to the from the project's pom.xml. You can override it and set it to anything you like here.

关于Spring WebApplicationInitializer 和 Jetty 8.x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9167554/

相关文章:

java - 如何添加有关 java 约束的附加信息

spring - spring boot的logging.config配置

java - 根 url 的匹配和静态资源的服务

java - 检查 Thymeleaf 中的 If-Else

java - 如何在 Spring Data JPA 中使用 After 和 Equals 创建方法名称?

java - hibernate 错误: No Persistence provider for EntityManager named my-unit

java - maven-jmod-plugin :3. 0.0-alpha-1 给出错误创建失败。非法参数异常

java - 在 Tomcat 7 上运行的 Web 应用程序中出现 NoClassDefFoundError

java - Spring不提供静态内容

spring - 如何定义多个initBinder