Running Jersey on Grizzly on Linux and Windows

天大地大妈咪最大 提交于 2019-12-12 09:37:32

问题


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

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)

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:

/*
 * 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

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>

回答1:


I am not using Maven. As of Today (26 July, 2013) I include the following jars to work with standalone grizzly server from my eclipse (Project | Run - no web.xml etc.)

  • 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

To download the files : head over to http://grizzly.java.net/dependencies.html and look for non maven developers files. The console server looks like

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;

    }

}



回答2:


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.




回答3:


You need to use a maven transformer to ensure that the service files are merged properly. See Grizzly and Jersey standalone jar



来源:https://stackoverflow.com/questions/11887321/running-jersey-on-grizzly-on-linux-and-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!