在Linux和Windows上运行Grizzly上的Jersey [英] Running Jersey on Grizzly on Linux and Windows

查看:268
本文介绍了在Linux和Windows上运行Grizzly上的Jersey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Windows .NET背景,但我正在尝试扩展我的专业知识,因此已经选择了一些Java项目。目前,我正在尝试创建一个REST API,所以我决定在这里浏览Jersey: http://jersey.java.net/nonav/documentation/latest/getting-started.html

I come from a Windows .NET background, but am trying to expand my expertise, and so have picked up a few Java projects. Currently, I'm trying to create a REST API, and so I decided to go through the walk through for Jersey here: http://jersey.java.net/nonav/documentation/latest/getting-started.html

我已经让Hello World项目在Windows中运行良好(使用NetBeans和Maven),但是当我尝试在Ubuntu中做同样的事情时(再次使用NetBeans和Maven)我收到以下错误:

I've gotten the Hello World project to work fine in Windows (using NetBeans and Maven), however when I try to do the same exact thing in Ubuntu (again using NetBeans and Maven) I get the following error:

Starting grizzly...
Aug 09, 2012 11:27:46 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  com.javarest.javarest2
Aug 09, 2012 11:27:47 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class com.javarest.javarest2.HelloWorldResource
Aug 09, 2012 11:27:47 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Exception in thread "main" java.lang.IllegalArgumentException: No container provider supports the type class org.glassfish.grizzly.http.server.HttpHandler
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:196)
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:134)
    at com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory.createHttpServer(GrizzlyServerFactory.java:242)
    at Main.startServer(Main.java:25)
    at Main.main(Main.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

我看过这篇文章:< a href =https://stackoverflow.com/questions/9787265/grizzly-and-jersey-standalone-jar> Grizzly和Jersey独立jar ,并修改了我的pom.xml以获得他的构建部分有,但我仍然得到同样的错误。我所拥有的代码几乎完全取决于示例,但我将在此处发布:

I've looked at this posting: Grizzly and Jersey standalone jar, and modified my pom.xml to have the build section that he had, but am still getting the same error. The code that I have is pretty much taken right out of the example, but I'll post it here:

HelloWorldResource.java:

HelloWorldResource.java:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javarest.javarest2;

import javax.ws.rs.*;

/**
 *
 * @author ryan
 */
// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class HelloWorldResource {

    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media
    // type "text/plain"
    @Produces("text/plain")
    public String getClichedMessage() {
        // Return some cliched textual content
        return "Hello World";
    }
}

Main.java

Main.java

import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.DefaultResourceConfig;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import org.glassfish.grizzly.http.server.HttpServer;

import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Main {

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost/").port(9998).build();
    }
    public static final URI BASE_URI = getBaseURI();

    protected static HttpServer startServer() throws IOException {
        System.out.println("Starting grizzly...");
        //ResourceConfig rc = new PackagesResourceConfig("com.sun.jersey.samples.helloworld.resources");
        ResourceConfig rc = new PackagesResourceConfig("com.javarest.javarest2");
        return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
    }

    public static void main(String[] args) throws IOException {
        HttpServer httpServer = startServer();
        System.out.println(String.format("Jersey app started with WADL available at "
                + "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...",
                BASE_URI, BASE_URI));
        System.in.read();
        httpServer.stop();
    }
}

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>com.javarest</groupId>
    <artifactId>JavaREST2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>JavaREST2</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-grizzly2</artifactId>
            <version>1.13</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-atom</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-atom-abdera</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-apache-client</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-spring</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-multipart</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-guice</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-simple-server</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
            <artifactId>oauth-client</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
            <artifactId>oauth-server</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
            <artifactId>oauth-signature</artifactId>
            <version>1.8</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.5</version>
                <configuration>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransfor‌mer"/>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <manifestEntries>
                                <Main-Class>com.javarest.javarest2.Main</Main-Class>
                                <Build-Number>1</Build-Number>
                            </manifestEntries>
                        </transformer>
                    </transformers>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>


推荐答案

好的,所以我现在拥有一台mac并想通了我会在那里尝试相同的程序。不出我的意料,我遇到了我在linux环境中遇到的同样问题。我有一个同事在他的机器上尝试相同的程序,但它对他来说很好。我注意到的唯一不同的是我得到了以下弹出窗口:

Ok, so I now own a mac and figured I would try the same procedure there. Not to my surprise, I ran into the same problem that I did in the linux environment. I had a co-worker try the same procedure on his machine, but it worked fine for him. The only thing that I noticed different when I did it was I got the following popup:

我一直只是点击确定。由于我的同事从未得到弹出窗口,我再次尝试了,这次点击取消。这似乎已经成功了。

I had always just clicked ok. Since my co-worker never got the popup, I tried it again, this time clicking cancel. That seems to have done the trick.

这篇关于在Linux和Windows上运行Grizzly上的Jersey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆