启动HTTP服务器时找不到Jersey ResourceConfig

时间:2014-04-08 15:22:56

标签: java rest jersey jax-rs

我已经编写了一些服务来通过REST提供乳胶文档的编译,现在它已经完全停止了。 :/

在开发它时,我使用TestNG和RestAssured测试它,它就像一个魅力,但现在我试图自己运行它。我不确定问题出在哪里,所以我尝试粘贴所有重要的东西并解释一下。

问题在于,当我让main方法中的服务器单独运行时,将发生以下异常。 (额外的问题:我是否可以以这种方式运行它,或者如果要作为战争部署在应用程序服务器上,它是否也可以从命令行以jar运行,只需使用java -jar x.jar调用它?)

控制台输出

JAVA_HOME=/usr/lib/jvm/java-7-oracle /usr/local/netbeans-7.4/java/maven/bin/mvn "-Dexec.args=-classpath %classpath de.uniluebeck.compilatex.RestServer" -Dexec.executable=java -Dexec.classpathScope=runtime org.codehaus.mojo:exec-maven-plugin:1.2.1:exec
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.

--- exec-maven-plugin:1.2.1:exec (default-cli) @ CompiLaTex ---
Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/jersey/server/ResourceConfig
        at de.uniluebeck.compilatex.RestServer.<init>(RestServer.java:31)
        at de.uniluebeck.compilatex.RestServer.main(RestServer.java:42)
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.server.ResourceConfig
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 2 more

这就是服务器实例化的类:

RestServer.java

public class RestServer {

    static final URI BASE_URI = URI.create("http://localhost:8080/");
    final HttpServer httpServer;

    public RestServer() {
        final ResourceConfig resourceConfig = new ResourceConfig(JobService.class, JobFileService.class, LatexEnvironmentService.class, MultiPartFeature.class);
        httpServer = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig);
        System.out.println(String.format("Jersey app started with WADL available at %sapplication.wadl\nTry out %sjobs or %slatex/environments",
                BASE_URI, BASE_URI, BASE_URI));
    }

    public void shutdown() {
        httpServer.shutdownNow();
    }

    public static void main (String[] args){
        RestServer restServer = new RestServer();
        try {
            System.in.read();
        } catch (IOException ex) {
            Logger.getLogger(RestServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        restServer.shutdown();
    }
}

我只是在四处寻找,但也许有一个问题,我只使用注释并且没有实际的web.xml?

JobService.java

@Path("/jobs")
public class JobService {

    private final RequestHandler requestHandler = new RequestHandler();

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response createJob() {
        return requestHandler.handleCreateJob();
    }

    @GET
    @Path("{jobId}/compile/{latexEnvironment}")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public Response compile(@PathParam("jobId") final Long jobId, @PathParam("latexEnvironment") final String latexEnvironment){ //, @Suspended final AsyncResponse asyncResponse) {
        return requestHandler.handleCompile(jobId, latexEnvironment);
    }

这就是 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>

    <groupId>de.uniluebeck</groupId>
    <artifactId>CompiLaTex</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>CompiLaTex</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
       <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>2.3.0</version>
            <exclusions>
                <!-- Exclude Groovy because of classpath issue -->
                <exclusion>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy</artifactId>
                </exclusion>
            </exclusions>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <!-- Needs to be the same version that REST Assured depends on -->
            <version>2.2.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-http</artifactId>
            <version>2.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbyclient</artifactId>
            <version>10.10.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>2.5.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-exec</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.gmbal</groupId>
            <artifactId>gmbal</artifactId>
            <version>4.0.0-b001</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.enunciate</groupId>
            <artifactId>enunciate-core</artifactId>
            <version>1.28</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>de.uniluebeck.compilatex.RestServer</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <url>http://download.eclipse.org/rt/eclipselink/maven.repo/</url>
            <id>eclipselink</id>
            <layout>default</layout>
            <name>Repository for library EclipseLink (JPA 2.1)</name>
        </repository>
    </repositories>
</project>

我很高兴有人看看这个问题,然后开始抨击它,谢谢你们! (也赞赏任何提示或链接到其他主题正确播放部署此类内容的方法。)

1 个答案:

答案 0 :(得分:1)

感谢@ andrei-i让我走上了正确的道路,我发现所有球衣依赖的范围都错了。被设置为 提供 时,他们必须在测试时一直在那里,但不是在运行时。我只是删除了所有这些范围,它的工作原理。天哪!