Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Jersey with the built in web server from Java SE 6?

I don't want to use Tomcat, Jetty or an Java EE 6 container to offer REST services but the built in web server.

like image 407
Mike Petterson Avatar asked May 22 '10 10:05

Mike Petterson


2 Answers

Ensure that you have Jersey's jersey-server.jar in the classpath, then it's as simple as:

HttpServer server = HttpServerFactory.create("http://localhost:9998/");
server.start();

Pick whatever port you want to use.

like image 73
BalusC Avatar answered Oct 16 '22 19:10

BalusC


For Jersey 2.x you will need jersey-container-jdk-http in your classpath. If you are using maven add this to your pom.xml:

<dependency>
     <groupId>org.glassfish.jersey.containers</groupId>
     <artifactId>jersey-container-jdk-http</artifactId>
     <version>2.9.1</version>
</dependency>

To start the server use this:

URI baseUri = UriBuilder.fromUri("http://localhost/").port(10000).build();
ResourceConfig resourceConfig=new ResourceConfig(WebService.class);
HttpServer httpServer=JdkHttpServerFactory.createHttpServer(baseUri, resourceConfig,true);
like image 36
Thunder Avatar answered Oct 16 '22 20:10

Thunder