我有一个包含多个模块的项目 .
其中两个生成war文件 .
-
一个war文件是一个REST应用程序,它提供了一些资源 .
-
Other war文件是一个与REST后端通信的Angular JS Web应用程序(仅限静态内容) .
出于演示目的,我想非常轻松地使用 mvn jetty:run
部署两个war文件
出于开发目的,我想从我的IDE(例如Eclipse Servers View)部署它们 .
当我通过启动服务器并将war文件复制到部署文件夹手动在单个Jetty服务器(v9.0.7.v20131107)上进行部署时,一切都会出现 .
当通过 mvn jetty:run
启动jetty时,两个war文件都会被部署,但不知何时REST资源不会被部署 .
我正在使用Jersey 2.手动部署时,我收到一条日志消息
Nov 14, 2013 10:44:37 PM org.glassfish.jersey.server.ApplicationHandler initialize INFO: Initiating Jersey application, version Jersey: 2.4 2013-10-24 18:25:49...
但是,以 mvn jetty:run
开头时,未显示此消息 . 所以我认为 Jersey 没有踢球 .
对于依赖注入我使用spring .
This is the parent pom in /pom.xml with the jetty-maven-plugin configuration
<project ...>
...
<build>
<plugins>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.7.v20131107</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
<contextHandlers>
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>module1/target/module1-${project-version}.war</war>
<contextPath>/module1</contextPath>
</contextHandler>
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>module2/target/module2-${project.version}.war</war>
<contextPath>/module2/contextPath>
</contextHandler>
</contextHandlers>
</configuration>
</plugins>
</build>
...
</project>
This is the pom for module1 (the REST module)
<parent>
...
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module1</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<springVersion>3.1.4.RELEASE</springversion>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.4</version>
</dependency>
<!-- Dependencies to internal modules -->
...
<!-- Depenencies to internal modules END -->
</dependencies>
</project>
This is the web.xml for module1
<web-app ...
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
This is the applicationContext.xml for module1
<beans ...>
<context:annotation-config/>
<context:component-scan base-package="com.stackoverflow.zip"/>
<aop:aspectj-autoproxy/>
</beans>
This is the Module1Application class for module1
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
@ApplicationPath("/rest")
public class Module1Application extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(Resource1.class);
classes.add(Resource2.class);
classes.add(MultiPartFeature.class);
return classes;
}
}
This is the pom for module2 (the AngularJS app) 非常轻:)
<project ...>
<parent>
...
</parent>
...
<modelVersion>4.0.0</modelVersion>
<artifactId>module2</artifactId>
<packaging>war</packaging>
</project>
您是否知道为什么Jersey应用程序在使用 mvn jetty:run
运行时未实例化但是在手动运行时?
我感谢对此主题的任何意见 .
亲切的问候 - 拉链
2 回答
您的javax.servlet版本在pom.xml中为
2.5
,但在web.xml中为3.0
. 尝试将其升级到pom.xml中的3.0
. IDE是否可能为您提供servlet 3.0?此外,由于Jetty提供了一个javax.servlet容器,请尝试将Servlet Api放入
pom.xml
中的提供范围内 . 像这样的东西:您可能希望在
web.xml
中添加<load-on-startup>1</load-on-startup>
元素 . 看看http://tutorials.jenkov.com/java-servlets/web-xml.html#load-on-startup这整个教程非常好 .
免责声明:我不熟悉Spring依赖关系正在做什么来帮助您的应用程序自举 . 您始终可以尝试删除单独分支上的依赖项,并在运行时将其重新添加
到目前为止,我还没有机会和Jersey 2.x一起比赛,但是从你在
web.xml
中看到的情况来看,看起来你没有正确设置Jersey .在非Spring应用程序中,您将具有以下内容:
在基于Spring的版本中,您将使用:
此外,检查您是否拥有这些Maven依赖项: