Skip to content

Instantly share code, notes, and snippets.

@seralf
Last active May 9, 2017 18:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seralf/f9c268dc7046ec436e29 to your computer and use it in GitHub Desktop.
Save seralf/f9c268dc7046ec436e29 to your computer and use it in GitHub Desktop.
Jetty9 / Jersey2 / minimal http server (with Java and Scala)
package example.server.embedded;
import java.net.URI;
import javax.ws.rs.core.UriBuilder;
import org.eclipse.jetty.server.Server;
import org.glassfish.jersey.jetty.JettyHttpContainerFactory;
import org.glassfish.jersey.server.ResourceConfig;
/**
* example for an embedded http server with scala and jetty. TODO: see how to
* define a basic root context, without introducing boilerplate
*/
public class MinimalHttpServer {
public static void main(String[] args) {
URI baseUri = UriBuilder.fromUri("http://localhost/").port(8888).build();
ResourceConfig config = new ResourceConfig(TestResource.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);
}
}
package web
import javax.ws.rs.core.UriBuilder
import example.server.embedded.TestResource
import org.glassfish.jersey.server.ResourceConfig
import org.glassfish.jersey.jetty.JettyHttpContainerFactory
/**
* example for an embedded http server with scala and jetty.
* TODO: see how to define a basic root context, without introducing boilerplate
*/
object MinimalHttpServer extends App {
val baseUri = UriBuilder.fromUri("http://localhost/").port(8888).build()
val config = new ResourceConfig(classOf[TestResource])
val server = JettyHttpContainerFactory.createServer(baseUri, config)
}
<dependencies>
<!-- jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.0.M1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-deploy</artifactId>
<version>9.2.0.M1</version>
</dependency>
<!-- jersey -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
package example.server.embedded;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* test it at http://localhost:8888/api-test/ask
*/
@Path("/api-test")
public class TestResource {
@GET
@Path("ask")
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "It's working! :-)";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment