4

I have downloaded a tutorial and modified it a little to suit my needs (added maven)

I was just wondering what makes the service start at a particular home page - when i run my service it defaults to the following

http://localhost:8080/RESTfulExample/WEB-INF/classes/com/ricki/test/JettyService.java

My web.xml looks as follows

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Restful Web Application</display-name>
    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.ricki.test</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

My jetty service class looks like this

import com.google.common.util.concurrent.AbstractIdleService;
import java.lang.management.ManagementFactory;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;

public class JettyService extends AbstractIdleService
{
    private Server server;

    @Override
    protected void startUp() throws Exception
    {
        server = new Server(8080);
        MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
        server.addBean(mbContainer);
        Resource resource = Resource.newClassPathResource("/webapp");
        WebAppContext context = new WebAppContext(resource.getURL().toExternalForm(), "/ricki-test/");
        server.setHandler(context);
        server.start();
    }

    @Override
    protected void shutDown() throws Exception
    {
        try
        {
            server.stop();
            server.join();
        }
        finally
        {
            server.destroy();
        }
    }
}

Any my rest class looks as follows

@Path("/hello")
public class HelloWorldService
{
    private final Logger logger = Logger.getLogger(HelloWorldService.class);

    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg)
    {
        logger.info("Received message " + msg);
        String output = "Hi : " + msg;
        return Response.status(200).entity(output).build();
    }
}

Ideall my homepage would be set to http://localhost:8080/RESTfulExample/ whcih displays my home page or in fact http://localhost:8080/RESTfulExample/rest/hello/ricki which allows me to interact with my service.

Thanks for your time.

Biscuit128
  • 5,218
  • 22
  • 89
  • 149

1 Answers1

11

There is no need to use a web.xml file if you don't want to. If you are using an embedded Jetty server, you can do the wireing to Jersey manually:

public static void main(String[] args) throws Exception {
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");

    Server jettyServer = new Server(8080);
    jettyServer.setHandler(context);

    ServletHolder jerseyServlet = context.addServlet(
         org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);

    // Tells the Jersey Servlet which REST service/class to load.
    jerseyServlet.setInitParameter(
       "jersey.config.server.provider.classnames",
       EntryPoint.class.getCanonicalName());

    try {
        jettyServer.start();
        jettyServer.join();
    } finally {
        jettyServer.destroy();
    }
}

Example from: http://www.nikgrozev.org/2014/10/16/rest-with-embedded-jetty-and-jersey-in-a-single-jar-step-by-step/

You can also use the jersey-container-jetty-http dependency:

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-jetty-http</artifactId>
    <version>2.23.1</version>
</dependency>

This allows you to do:

URI baseUri = UriBuilder.fromUri("http://localhost/").port(9998).build();
ResourceConfig config = new ResourceConfig(MyResource.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);

If you really want to use web.xml, you should access it in a different fashion:

Server server = new Server(8080);

String rootPath = SimplestServer.class.getClassLoader().getResource(".").toString();
WebAppContext webapp = new WebAppContext(rootPath + "../../src/main/webapp", "");
server.setHandler(webapp);

server.start();
server.join();

See also: Configure embedded jetty with web.xml?

At that point, it is easier to use the Jetty maven plugin, which bundles your war file and deploys it to a local Jetty server:

        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.3.6.v20151106</version>
            <configuration>
                <scanTargets>
                    <scanTarget>${project.basedir}/src/main</scanTarget>
                    <scanTarget>${project.basedir}/src/test</scanTarget>
                </scanTargets>
                <webAppConfig>
                    <contextPath>/${project.artifactId}-${project.version}</contextPath>
                </webAppConfig>
                <contextPath>${project.artifactId}</contextPath>
            </configuration>
        </plugin>
Community
  • 1
  • 1