Skip to content

LayingInYourFirstHammock

John D. Ament edited this page Aug 21, 2017 · 13 revisions

Referencing

Hammock is made available through Maven Central. You can add it to your project as a dependency, the easiest way is to start from the Microprofile distribution:

<dependency>
    <groupId>ws.ament.hammock</groupId>
    <artifactId>dist-microprofile</artifactId>
    <version>${hammock.version}</version>
</dependency>

This will give you a basic application structure that will include the necessary dependencies to run Hammock - a Servlet Container (Undertow), a JAX-RS runtime (Apache CXF), a JSON-P library (Apache Johnzon) and a CDI runtime (Weld). If at this point, you just run the main class ws.ament.hammock.Bootstrap you'll get a running container. Now this won't do a whole lot, so let's add our first REST endpoint.

@Path("/hello")
@RequestScoped
public class HelloWorldEndpoint {
    @GET
    public Response doGet() {
        return Response.ok("Hello, world!").build();
    }
}

If you're not familiar with JAX-RS, it might be best to read through the RestEasy User Guide. This exposes a single REST endpoint available at /hello and returns back a simple Hello, world! response. If you now run your application and access http://localhost:8080/hello you'll get this out. Cool!

Packaging your app

I'll be honest, I could write a custom maven or gradle plugin to do packaging, but I'd prefer to leverage what's available already.

The best way to package your app, if you're using maven, is to leverage the shade plugin to build an uber-jar. The configuration is pretty straight forward:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>hammock</shadedClassifierName>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <!-- this next block only needed for CXF -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/cxf/bus-extensions.txt</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>ws.ament.hammock.Bootstrap</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

You'll now have an executable JAR file, just run java -jar target/yourapp-hammock.jar and your service will be available on http://localhost:8080/hello

Capsules

You can also use Capsule to package your application. You can add this plugin to your build

<plugin>
    <groupId>com.github.chrisdchristo</groupId>
    <artifactId>capsule-maven-plugin</artifactId>
    <version>1.4.3</version>
    <executions>
        <execution>
            <goals>
                <goal>build</goal>
            </goals>
            <configuration>
                <appClass>ws.ament.hammock.Bootstrap</appClass>
                <type>fat</type>
            </configuration>
        </execution>
    </executions>
</plugin>

And your build will now leverage Capsule instead of a shaded JAR.

Next, Configure your application or Bootstrap your application

Clone this wiki locally