10

I have a simple project to test JAX-RS services. Yesterday I downloaded jersey-1.7.1 and used com.sun.jersey.api.container.httpserver.HttpServerFactory and com.sun.net.httpserver.HttpServer to create a http server to test my services from inside eclipse (w/o a heavy weight container)

Today, I downloaded the latest jersey jars (jaxrs-ri) and the HttpServerFactory is missing. Looks like they removed the class between 1.7.1 => 2.0 but I cannot find it in deprecated section. I see grizzly2 classes in API section (maybe that is what I am supposed to use now) but none of the jars in jaxrs-ri bundle provide those classes. I downloaded the jersey-grizzly 1.12 jar but it only has com/sun/jersey/server/impl/container/grizzly2/GrizzlyContainer classes and no implementation.

So question to kind souls

1 - with the latest jaxrs-ri jars from jersey download page, what is the recommended way to create a simple http server to test from command line if the 1.7.1 way has been deprecated. what jars to download/include and perhaps a short code sample?

2 - The whole documentation around creating a simple REST service using java is a big mess. So how do you find the right information?

(Really, this is not a joke. maybe this would require a separate blog post - just look at the mess, changed API, no information on deprecated features, implementation diffs, e.g. CXF vs. jersey, API 1.x vs API 2.0, Glassfish container v x.y vs. Tomcat, version x of y vs. version x2 of y, servlet 3.0 would have this file, are you extending Application or not!)

Update

working example with JDKHttp server

package test.jersey;

import java.net.URI;
import com.sun.net.httpserver.HttpServer;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory ;
import org.glassfish.jersey.server.ResourceConfig;

public class ConsoleServerV2 {

    static final String BASE_URI = "http://localhost:9099/";

    public static void main(String[] args) throws Exception {
        HttpServer server = null ;

        ResourceConfig rc = new ResourceConfig(rest.Service.class);
        URI endpoint = new URI(BASE_URI);

        server = JdkHttpServerFactory.createHttpServer(endpoint,rc);
        System.out.println("console v2.0 : Press Enter to stop the server. ");
        System.in.read();
        server.stop(0);

    }
}
rjha94
  • 4,292
  • 3
  • 30
  • 37

3 Answers3

10

You could use the JdkHttpServerFactory, which is available in the jersey-container-jdk-http-2.0.jar:

ResourceConfig rc = new ResourceConfig(HelloWorldResource.class);
HttpServer server = JdkHttpServerFactory.createHttpServer(baseUri, rc);

No need to call server.start()!

stck0177
  • 404
  • 4
  • 7
  • thanks. probably this is the path of least resistance. I have updated the question with a working sample. – rjha94 Jul 18 '13 at 15:47
  • I'm trying this and running into NoClassDefFoundError: com/google/common/base/Function at runtime. – ash Apr 19 '14 at 02:52
  • found it here, http://mirrors.ibiblio.org/pub/mirrors/maven2/com/google/collections/google-collections/0.9/google-collections-0.9.jar, but that appears to be a rabbit hole... another missing dependency. Bah. – ash Apr 19 '14 at 02:55
2

Pls try this Grizzly 2 bundle http://search.maven.org/remotecontent?filepath=org/glassfish/grizzly/grizzly-http-servlet-server/2.3.3/grizzly-http-servlet-server-2.3.3.jar

alexey
  • 1,959
  • 10
  • 9
  • hi @alexey, i will try that. do you have a code sample to test jersey with grizzly embedded server as well? – rjha94 Jul 18 '13 at 06:25
  • sure, there are lot's of samples in Jersey workspace, for example helloworld: https://github.com/jersey/jersey/tree/master/examples/helloworld – alexey Jul 18 '13 at 07:04
  • sorry but one more question, why grizzyly-http-servlet-server, is servlet necessary? Can I not simply work with the http server? – rjha94 Jul 18 '13 at 07:34
  • servlet is not necessary, just wanted to be safe in case you needed it. Here is plain httpserver bundle: http://search.maven.org/remotecontent?filepath=org/glassfish/grizzly/grizzly-http-server-core/2.3.3/grizzly-http-server-core-2.3.3.jar – alexey Jul 18 '13 at 14:16
0

You can also get the bundle from here http://mvnrepository.com/artifact/com.sun.jersey/jersey-server/1.2 Just add it to your library folder then right click on it in the project and select "Add as a library."

sherebry
  • 184
  • 1
  • 3