Setting up JAX-RS to provide an API and serve up static resources

587 views Asked by At

Let me me first explain my requirements. I'm trying to build a JAX-RS application with embedded jetty server to serve up:

/    => Single Page Application (static index.html, JS, images )

/api => Driving API (JAX-RS resources)

The problem I've been having is that not with the API, but rather the static resources. My current configuration is this:

public static void main( final String[] args ) throws Exception
{
    // create our server on port 4567
    final Server server = new Server( 4567 );

    // create a default context handler for our server to root path with sessions enabled
    final ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS );
    // our root static path

    // create container for JAX-RS servlet resources mapped with configuration
    final ServletContainer servletContainer = new ServletContainer( new MyConfig() );
    // create our servlet holder for our multiple JAX-RS resources
    final ServletHolder apiServletHolder = new ServletHolder( servletContainer );
    // map our JAX-RS resources to begin at root route
    context.addServlet( apiServletHolder, "/api/*" );

    final ServletHolder staticServlet = new ServletHolder( new DefaultServlet() );
    staticServlet.setInitParameter( 
        "resourceBase", Main.class.getClassLoader().getResource( "." ).toString() );
    context.addServlet( staticServlet, "/" );

    server.setHandler( context );
    server.start();
    server.join();
}

My folder configuration is as follows:

├───main
│   ├───java
│   │   └───com
│   │       └───bar
│   │           └───foo
│   │               └───controllers
│   └───static
│       └───images
└───test
    └───java
        └───com
            └───bar
                └───foo

My POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.foo.bar</groupId>
    <artifactId>jettyApp</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>jettyApp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-annotations</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-http</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.bar.foo.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <!-- add Main-Class to manifest file -->
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>com.foo.bar.Main</Main-Class>
                                        <Class-Path>.</Class-Path>
                                        <Build-Number>123</Build-Number>
                                    </manifestEntries>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <properties>
        <jetty.version>9.3.0.M2</jetty.version>
        <jersey.version>2.7</jersey.version>
    </properties>
</project>

When I create a jar with shade though, the application fails as the location of the static resources display the folder that I'm running the application from:

$ pwd
/home/workspace/example
$ java -jar example.jar
# server starts
$ curl localhost:4567
# tree of /home/workspace/example

This is no doubt due to setting the resource base in Main.class.

My Question:

How do I package this and configure this in a way that it works for both IDE and standalone uber jar?

Thanks in advance :)

0

There are 0 answers