Wednesday, March 18, 2015

PI with Java embedded Jetty for serving JSP or plain html and exposing Jersey REST services

Why? because I like embedding everything in one app in a more lightweight fashion.

First, make sure you have the Oracle 1.7 JDK instead of the delivered one with Debian, plenty of articles out there to hold your hand on that one.

Install Eclipse, it really helps to debug, and is quick enough to develop on.

Install Maven in Eclipse, I tried without and ended up in jar hell, so take the easier route.

POM dependencies :-
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey-version}</version>
        </dependency>
        <dependency>
   <groupId>org.glassfish.jersey.core</groupId>
   <artifactId>jersey-client</artifactId>
   <version>${jersey-version}</version>
</dependency>
<dependency>
   <groupId>org.glassfish.jersey.connectors</groupId>
   <artifactId>jersey-jetty-connector</artifactId>
   <version>${jersey-version}</version>
</dependency>
<dependency>
 <groupId>com.fasterxml.jackson.jaxrs</groupId>
 <artifactId>jackson-jaxrs-json-provider</artifactId>
 <version>2.3.0</version>
</dependency>
        <dependency>
   <groupId>org.glassfish.jersey.containers</groupId>
   <artifactId>jersey-container-servlet</artifactId>
   <version>${jersey-version}</version>
</dependency>
        <dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http-spi</artifactId>
<version>${jettyVersion}</version>
</dependency>
        <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>${jettyVersion}</version>
   </dependency>
        <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-webapp</artifactId>
      <version>${jettyVersion}</version>
   </dependency>
        <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-security</artifactId>
      <version>${jettyVersion}</version>
   </dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<version>${jettyVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jettyVersion}</version>
</dependency>
<dependency>
  <groupId>org.eclipse.jetty</groupId>
 <artifactId>apache-jsp</artifactId>
<version>${jettyVersion}</version>
</dependency>
    </dependencies>
    <properties>
        <jersey-version>2.7</jersey-version>
        <jettyVersion>9.3.0.M2</jettyVersion>
    </properties>

Make sure the jetty version is above 9.2 as the JSP compiler changed from glassfish to apache.

Do your standard jetty server object create on the desired port, the only thing you need to do to get JSP files served now is :-
              // Create WebAppContext for JSP files.
              WebAppContext webAppContext = new WebAppContext();
              webAppContext.setContextPath("/jsp");
              webAppContext.setResourceBase("/home/pi/Jetty/");
              webAppContext.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());

//Now for the JAX-WS-RS (RESTful) resources
ServletContextHandler contextRS = new           ServletContextHandler(ServletContextHandler.SESSIONS);
contextRS.setContextPath("/RS");
  ServletHolder h =new ServletHolder(new ServletContainer());
       ServletHolder jerseyServlet = contextRS.addServlet(
               org.glassfish.jersey.servlet.ServletContainer.class, "/*");
          jerseyServlet.setInitOrder(1);      jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",RESTService.class.getCanonicalName());
//RESTService is just a Jersey annotated class, standard stuff.

Add the handlers to the handler collection. Job done!.

It took me quite a while to get this, as there is a myriad of older deprecated garbage to sift through.
The key ingredient to JSP was the InstanceManager. I have other simple handlers, lifted straight from the online examples to return on the fly html content using other context's that just work out of the box.

No comments:

Post a Comment