처음 Jax-RS webservice를 작성하기 시작했습니다. 어제부터 maven을 사용하여 프로젝트를 빌드하면 자동으로 테스트 서버를 시작할 수있는 방법을 찾으려고합니다. 나는 몇 가지 테스트를 실행하려고하지 않고 다음 명령을 실행하면, 모든 작동합니다 : 5000/서비스/사용자 :통합 테스트 시작 서버가 실패합니다
mvn clean install
export PORT=5000
java -cp target/classes:"target/dependency/*" net.avedo.spozz.Spozz
이 서비스는 로컬 호스트에서 다음을 사용할 수 있습니다.
:이 코드 조각에 해당testHasUser(net.avedo.spozz.models.UserTest): Connection refused
: 나는 서버를 시작하려고 내가이 게시물의 끝 부분에 부착 된 pom.xml 파일을 사용하여 자동으로 테스트를 실행한다면, 오류가 발생합니다
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://localhost:5000/services/users/1") ;
response = httpClient.execute(httpGet) ;
그래서 여기에 무슨 문제가 있는지 잘 모르겠습니다. 나는 서버가 잘못된 URI 아래서 시작되었다고 생각하지만, 테스트 서버가 시작된 곳을 말하지 않았으므로이를 확인할 수는 없다. 프로젝트에 대한 테스트를 실행하기 위해 테스트 서버를 실행하려면 pom.xml 파일에서 추가 또는 변경해야 할 사항을 알아야합니다.
|-pom.xml
|-src
|---main
|-----java
|-------net
|---------avedo
|-----------spozz
|-------------Spozz.java
|-------------models
|---------------User.java
|-------------services
|---------------UserResource.java
|-----resources
|-----webapp
|-------index.html
|-------WEB-INF
|---------web.xml
|---test
|-----java
|-------net
|---------avedo
|-----------spozz
|-------------models
|---------------UserTest.java
|-----resources
|-target
|---classes
|-----net
|-------avedo
|---------spozz
|-----------Spozz.class
|-----------models
|-------------User.class
|-----------services
|-------------UserResource$1.class
|-------------UserResource$2.class
|-------------UserResource.class
|---test-classes
|-----net
|-------avedo
|---------spozz
|-----------models
|-------------UserTest.class
내 기존의 pom.xml
<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>
<groupId>net.avedo.spozz</groupId>
<artifactId>Spozz-Service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spozz REST Webservice</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>7.6.0.v20120127</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>7.6.0.v20120127</version>
</dependency>
<!-- Jersey -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
<!-- jUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>5000</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals><goal>copy-dependencies</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!-- M2Eclipse Compatibility -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[2.4,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
\\ EDIT (1) :
내 pom.xml
에 업데이트가 도움이 될 수로, 여기에 내 프로젝트 구조입니다 최신 버전의 부두를 사용하지만 지금은 새로운 오류가 발생합니다.
2014-03-06 11:51:29.126:WARN:oejuc.AbstractLifeCycle:FAILED [email protected]:
java.lang.NoClassDefFoundError: org/eclipse/jetty/servlet/FilterMapping
는 또한 포트 문제는 다시 팝업 :
Error binding monitor port 8080: java.net.BindException: Address already in use
2014-03-06 11:51:24.909:INFO:oejs.Server:jetty-8.1.14.v20131031
2014-03-06 11:51:25.245:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
2014-03-06 11:51:26.839:WARN:oejsh.RequestLogHandler:!RequestLog
2014-03-06 11:51:26.847:INFO:oejs.AbstractConnector:Started [email protected]:28080
내 업데이트의 pom.xml
<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>
<groupId>net.avedo.spozz</groupId>
<artifactId>Spozz-Webservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>9.1.0.v20131115</jetty.version>
<jersey.version>1.8</jersey.version>
<junit.version>4.11</junit.version>
<apache.commons.version>1.3.2</apache.commons.version>
<apache.http.version>4.3.2</apache.http.version>
<jsp.version>2.5</jsp.version>
<maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version>
<sql.maven.plugin.version>1.5</sql.maven.plugin.version>
<postgresql.jdbc.version>9.1-901.jdbc4</postgresql.jdbc.version>
</properties>
<dependencies>
<!-- Jetty -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>${jetty.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>org.eclipse.jdt.core</artifactId>
<groupId>org.eclipse.jetty.orbit</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- Jersey -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- jUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${apache.commons.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apache.http.version}</version>
<scope>test</scope>
</dependency>
<!-- PostgreSQL -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.jdbc.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.14.v20131031</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>8080</stopPort>
<stopWait>10</stopWait>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>28080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>test-compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>${sql.maven.plugin.version}</version>
<dependencies>
<!-- Specify the dependent jdbc driver here -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.jdbc.version}</version>
</dependency>
</dependencies>
<!-- Common configuration shared by all executions -->
<configuration>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432:spozz_db</url>
<username>postgres</username>
<password>root</password>
<!-- You can comment out username/password configurations and have
maven to look them up in your settings.xml using ${settingsKey} -->
<settingsKey>sensibleKey</settingsKey>
<!-- All executions are ignored if -Dmaven.test.skip=true -->
<skip>${maven.test.skip}</skip>
</configuration>
<executions>
<execution>
<id>drop-schema-before-test-if-any</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<!-- Need another database to drop the targeted one -->
<url>jdbc:postgresql://localhost:5432:postgres</url>
<autocommit>true</autocommit>
<sqlCommand>DROP SCHEMA spozz CASCADE</sqlCommand>
<!-- Ignore error when database is not available -->
<onError>continue</onError>
</configuration>
</execution>
<execution>
<id>create-schema</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<autocommit>true</autocommit>
<srcFiles>
<srcFile>src/main/sql/spozz-schema.sql</srcFile>
</srcFiles>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>dbunit-maven-plugin</artifactId>
<version>1.0-beta-3</version>
<dependencies>
<!-- Specify the dependent jdbc driver here -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.jdbc.version}</version>
</dependency>
</dependencies>
<!-- Common configuration shared by all executions -->
<configuration>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432:spozz_db</url>
<username>postgres</username>
<password>root</password>
<!-- You can comment out username/password configurations and have maven
to look them up in your settings.xml using ${settingsKey} -->
<settingsKey>sensibleKey</settingsKey>
<!-- All executions are ignored if -Dmaven.test.skip=true -->
<skip>${maven.test.skip}</skip>
</configuration>
<executions>
<execution>
<phase>test-compile</phase>
<goals>
<goal>operation</goal>
</goals>
<!-- Specific configurations -->
<configuration>
<type>CLEAN_INSERT</type>
<src>src/test/resources/spozz_db_testdata.xml</src>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals><goal>copy-dependencies</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!-- M2Eclipse Compatibility -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[2.4,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
jetty가'test-compile'에서 시작하고'test' 단계에서 중지되도록 my * pom.xml * 파일을 업데이트했습니다. 그러나 이제는 두 가지 새로운 오류가 발생합니다. '실패 : 주소는 이미 사용 중'과'java.lang.NoClassDefFoundError : org/apache/jasper/runtime/JspApplicationContextImpl'이지만'ps -ef | grep 5000 | xargs kill'은 포트 5000에서 실행중인 서비스가 취소되지 않음을 나타냅니다. '[WARNING] failed [email protected] : 8080 java.net.BindException : 주소가 이미 사용 중입니다. – Phidelux
디버그 모드에서 Maven을 실행하면 올바르게 호출하면 포트 5000에서 Maven이 시작됩니다. Jetty를 8080 또는 다른 것으로 시작하십시오. – carlspring
감사합니다. 다른 포트로 전환하고 단계를 변경하면 도움이되었습니다. 그러나, 나는 아직도 JSP를 포함한 문제가 있습니다. 따라서, 새 버전의 jetty를 사용하기 위해'pom.xml' (내 게시물에 추가)을 업데이트했습니다. 이제 새로운 오류가 발생합니다. 그래서 저는 GitHub에 가서 다른 프로젝트를 찾아 보았습니다. ([https://github.com/jetty-project/embedded-jetty-jsp]). 새 버전에서는 프로젝트의'webapp/WEB-INF' 폴더에'web.xml'을 제공 할 필요가없는 것 같아서 혼란 스럽습니다. 그래서 이것이 JSP를 사용하는 JAX-RS 기반 웹 서비스를 구현하는 새로운 방법인지 궁금합니다. – Phidelux