Building a Kotlin Dropwizard Maven REST API from scratch: Part 4

Part 4: Running

We have configured our dependencies, wrote and configured a Kotlin Dropwizard resource and tested it. Now we need to run it! If you are using an IDE, open the file App.kt and you should be able to right click or to the left be able to run the @JvmStatic fun main method.You should see some text come up similar to the following:

usage: java -jar project.jar [-h] [-v] {server,check} ...

positional arguments:
  {server,check}         available commands

What this means is we need to tell the Dropwizard app to run as a server, and where to find the configuration properties file. To configure the IDE to run correctly, click on the Run Configuration dropdown on the top right and select Edit Configurations… In the Program Arguments section enter: server src/main/resources/app-config.yml then click the Ok button. Now when you run it again, you should see something similar to the successful message below.

INFO  io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:

    GET     /echo (com.mannanlive.dropwizardkotlinapp.resources.EchoResource)

INFO  org.eclipse.jetty.server.AbstractConnector: Started application@7bebcd65{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
INFO  org.eclipse.jetty.server.AbstractConnector: Started admin@47447ccf{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
INFO  org.eclipse.jetty.server.Server: Started @4353ms

This is telling us a couple of things. Firstly it found the EchoResource we created and has correctly mapped a GET endpoint to /echo. Secondly it has told us that it is currently running over HTTP on port 8080. To test it out, we will send a curl command to this endpoint. Open a terminal or command prompt and run the following:

> curl http://localhost:8080/echo

{"query":"default","prop":"abc","number":123,"bool":false,"array":[456,789],"map":{"rate":11.5},"date":"31-01-2019 10:20:42"}

Congratulations, you can pat yourself on the back for successfully writing and building a Kotlin Dropwizard REST endpoint. You can click on the Run Configuration and click Save Configuration so that these configuration will always be available whenever you need it. If you are having trouble calling it from curl thats ok, because it’s a GET endpoint it’s easy to test in other ways. For instance you can open it up in an internet browser by visiting: http://localhost:8080/echo

This is really helpful for local development, for instance you can make changes and re-run (⌘R) to test them out. However this isn’t a feasible method to deploy and run it in a production-esque environment. To do this, we need to package it up in an easily deployable and runnable way. Luckily there is another maven plugin that can help us. Open up your pom.xml file and just before </plugins> add the following XML.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <finalName>${project.artifactId}</finalName>
        <filters>
            <filter>
                <artifact>*:*</artifact>
                <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                </excludes>
            </filter>
        </filters>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.mannanlive.dropwizardkotlinapp.App</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

Again let’s break it down. Firstly this is including the Maven Shade plugin that creates what are known as “Fat JARs”. A JAR (Java Archive) is just a special zipped file (you can treat them as such) that includes some additional meta information so Java knows how to interact with them. Typically jar files just contains the code related to the library, and you need to use dependency management (like Maven) to manage all the dependencies. A fat jar is the opposite and includes everything.

We then tell it to use the project artifact id as the final name of the jar. Thirdly we tell the jar file to exclude all digital signatures, otherwise Java will think the file is invalid. Next we combine all META-INF/service information (the meta data mentioned earlier) together so that everything still works. Lastly, and most importantly, we tell the jar where the entry point to the application is. Remember that @JvmStatic fun main method that we created and running above? That is also going to be the entry point for the jar.

> mvn deploy
> java -jar target/dropwizard-kotlin-app.jar server src/main/resources/app-config.yml

Running the above two commands in your project’s folder will first invoke Maven to create the jar named dropwizard-kotlin-app.jar in the target folder (default output folder). Then we invoke Java with jar option, pointing to our jar file and passing in both the server command and the location of the configuration file. If everything is working correctly you should see that our Dropwizard app has started on port 8080 and you should be able to request the /echo endpoint to see it all working correctly.

However this RESTful endpoint is not doing much at the moment. Most micro-services have to connect to other applications or a database in order to do something useful. So in the next part, let us use dropwizard-client in order to communicate to another service.

About the Author

Mannan

Mannan is a software engineering enthusiast and has been madly coding since 2002. When he isn't coding he loves to travel, so you will find both of these topics on this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *