我有一个(Maven)多模块项目,它在Web应用程序模块中设置了Jersey,在集成测试模块中进行了集成测试。泽西岛通过ResourceConfig
类配置为
public class Application extends ResourceConfig {
public Application() {
register(RequestContextFilter.class);
packages("foo.bar.baz", "com.fasterxml.jackson.jaxrs.base");
register(ValidationFeature.class);
}
}
并设置为web.xml
中的部署
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring/*.xml</param-value>
</context-param>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>foo.bar.baz.application.Application</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/foo_bar_service/*</url-pattern>
</servlet-mapping>
</web-app>
这两个文件都位于web-app模块中。构建项目并在Tomcat上部署它可以完美地运行。
对于集成测试我在Maven Jetty插件中通过Maven Jetty插件在预集成测试阶段启动Jetty,将web-app的源目录指向web-app模块。 pom.xml的相关部分
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- The Basics -->
<artifactId>integration-test</artifactId>
<parent>
<groupId>foo.bar</groupId>
<artifactId>parent</artifactId>
<version>2.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<packaging>jar</packaging>
<properties>
<path>${project.parent.basedir}</path>
<checkstyle.basedir>${project.parent.basedir}/build/rules</checkstyle.basedir>
<pmd.config.location>${project.parent.basedir}/build/rules/pmd_rules.xml</pmd.config.location>
</properties>
<dependencies>
<dependency>
<groupId>foo.bar</groupId>
<artifactId>webapp</artifactId>
<type>war</type>
</dependency>
<!-- Testing -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
</dependencies>
<!-- Build Settings -->
<build>
<plugins>
<!-- Maven Checkstyle Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
<!-- Maven JAR Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<!-- Maven Compiler Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<!-- Maven Resources Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<!-- Jetty plugin -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<daemon>false</daemon>
<scanIntervalSeconds>0</scanIntervalSeconds>
<stopKey>jettystop</stopKey>
<stopPort>9999</stopPort>
<useTestClasspath>true</useTestClasspath>
<webAppSourceDirectory>${project.parent.basedir}/mobile-api-svc-webapp/src/main/webapp</webAppSourceDirectory>
<webXml>${project.parent.basedir}/mobile-api-svc-webapp/src/main/webapp/WEB-INF/web.xml</webXml>
<contextPath>/</contextPath>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Maven Failsafe Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
NB!为了测试jetty服务器,我将daemon
设置为false,以便我可以从浏览器ping servlet。
使用mvn verify
启动Jetty服务器后,我注意到Jersey没有获取资源类(它们也驻留在web-app模块中),因此正确的url-s会给出404 Not Found。
我怀疑包裹扫描的问题似乎得到了证实。修改ResourceConfig
后,通过添加泽西岛处理的registerClasses(RESOURCE_CLASSES);
网址正确工作。
我的问题是(我没有找到答案):
- 是否可以修复包扫描以跨模块工作?
- 有没有办法用两个ResoureConfig
文件配置相同的Jersey servlet,这样我就可以在集成测试模块中放置注册类的丑陋黑客?