java - 在 Linux 和 Windows 上的 Grizzly 上运行 Jersey

标签 java ubuntu netbeans jersey grizzly

我有 Windows .NET 背景,但我正在努力扩展我的专业知识,因此选择了几个 Java 项目。目前,我正在尝试创建一个 REST API,所以我决定在这里完成 Jersey 的演练:http://jersey.java.net/nonav/documentation/latest/getting-started.html

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

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)

我看过这个帖子:Grizzly and Jersey standalone jar ,并修改了我的 pom.xml 以包含他所拥有的构建部分,但我仍然遇到相同的错误。我所拥有的代码几乎是从示例中取出来的,但我会把它贴在这里:

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";
    }
}

主.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>

最佳答案

我没有使用 Maven。截至今天(2013 年 7 月 26 日),我包括以下 jar 以与我的 eclipse 中的独立灰熊服务器一起使用(项目 | 运行 - 无 web.xml 等)

  • grizzly-framework-2.3.4.jar
  • grizzly-http-2.3.4.jar
  • grizzly-http-server-2.3.4.jar
  • jersey-container-grizzly2-http-2.0.jar

要下载文件:前往 http://grizzly.java.net/dependencies.html并寻找非 Maven 开发人员文件。控制台服务器看起来像

package test;

import java.io.IOException;
import java.net.URI;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.grizzly.http.server.HttpServer;


public class GrizzlyServer {

    private static final URI BASE_URI = URI.create("http://localhost:9090/service/");

    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws IOException {


            final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, createApp());
            System.out.println(String.format("Application started.%nHit enter to stop it..."));
            System.in.read();
            server.stop();
    }

    public static ResourceConfig createApp() {
        ResourceConfig rc = new ResourceConfig()
        .packages("test")
        .register(JacksonFeature.class);

        return rc;

    }

}

关于java - 在 Linux 和 Windows 上的 Grizzly 上运行 Jersey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11887321/

相关文章:

java - 使用现有 PDF 作为模板创建具有多个页面的新 PDF

ubuntu - 如何读取/转换 pcm,嵌入式缩短编码?

java - 在 IDE Netbeans 中从 java 连接到 MySQL 数据库

javascript - 如何使用 cron 从 bash 运行 Node 脚本

ubuntu - 使用自定义 OpenSSL 构建 OpenVPN

android - 无法在 Netbeans 上编译 Android hello world

java - 无法在 Netbeans 中将项目从 Tomcat 运行到 Glassfish 服务器

Java并发场景——需要同步还是不需要?

java - 为什么我的 Set 在 Java 编程中充当数组列表?

java - 在 GUI 文本区域添加和刷新文本?