Pantelis Natsiavas
Pantelis Natsiavas

Reputation: 5369

Embed jersey in java application

I am trying to embed jersey server in my java application so that I could control programmatically its starting/stopping etc. I have found this tutorial but it is for jetty 1.18.1 (rather old). Using the latest jersey version (2.22.1) it seems that there have been many changes in classes, namespaces etc and the example shown in the above tutorial is not working.

Could someone indicate a way of embedding last version of jersey in a custom application? I believe it is doable but I cannot seem to find a reference, documentation, example or whatever...

Upvotes: 2

Views: 1681

Answers (2)

Archimedes Trajano
Archimedes Trajano

Reputation: 41682

If you want to embed it on a lower level API in case you choose to change the web engine to something like Netty or Vert.X You have to go to org.glassfish.jersey.servlet.WebComponent

You need to construct it with a WebConfig and you would need to create your own implementation of the HttpServletRequest/Response/Context classes.

You can go lower by building your own ApplicationHandler, but at that stage it's pretty much Jersey internals. On the WebComponent level you still have the servlet API so it is easier to build what you need.

Upvotes: 0

Pantelis Natsiavas
Pantelis Natsiavas

Reputation: 5369

As Gimby suggested, the documentation provides the answer. Jersey can be embedded in a java application, using another http server (such as jetty). In my case, I built a very simple working example project with these three steps:

  1. Added the respective maven dependencies in my pom.xml. Please note that I only have a maven dependency to the jetty connector of the jersey project. I don't have a dependency on the jetty server itself. Having both of them would cause errors.

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.22.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
        <version>2.22.1</version>
    </dependency>
    
  2. Built a simple REST resource class

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    @Path("testingservice/")
    public class TestingRESTService {
    
        @GET
        @Path("{requestID}")
        @Produces(MediaType.TEXT_PLAIN)
        public String get(@PathParam("requestID") String requestID) {
            return "\n This is request with ID "+requestID;
        }
    }
    
  3. In the main function added the code to start the embedded REST server

    import java.io.IOException;
    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;
    
    @SuppressWarnings("restriction")
    public class TestingApplicationStartingRESTServerProgrammatically {
    
        public static void main(String[] args) throws Exception {
            URI baseUri = UriBuilder.fromUri("http://localhost/").port(9998).build();
            ResourceConfig config = new ResourceConfig(CrunchifyAPI.class);
            Server server = JettyHttpContainerFactory.createServer(baseUri, config);
            server.start();
        }
    
    
    }
    

Upvotes: 2

Related Questions