REST on a cloud: easily package a JAX-RS application to deploy to Kubernetes with Appsody

David Harris
Jul 15, 2019 · 4 min read
Container ship being loaded
Container ship being loaded

In this tutorial I’ll show how you can use Appsody to take care of packaging applications for the cloud, so that you can focus on writing code. You’ll learn using the application provided in the OpenLiberty guide for ‘Creating a RESTful Web Service’, although you’ll be able to use the same approach for other Java applications based on Eclipse Microprofile.

What is Appsody?

Image for post
Image for post

Appsody is an open source project which simplifies developing apps for the cloud. It helps teams share common foundations on which to build applications; where they can embed their expertise and standards, and reduce the learning curve before new folks can start writing apps.

At its core, Appsody provides:

  • Stacks which include language runtimes, frameworks, libraries and tools.
  • Project Templates which build upon these Stacks and provide an initial project structure, and just enough code to get started with your own “cloud native” application.
  • A CLI which allows you to manage the full development lifecycle.

Applications can be iteratively developed whilst deployed in a locally running docker container, with code changes automatically detected and reflected in the app.

This tutorial will show you how existing applications can be built on top of a Stack to take advantage of Appsody’s workflows for development and deployment.

Pre-requisites

  • You will also need Docker and Maven on your workstation.

Sample application

To work with the finished application, clone the project

git clone https://github.com/OpenLiberty/guide-rest-intro

and navigate to /guide-rest-intro/finish where the completed application resides.

Setting up the local development environment

The JAX-RS and JSON-P features used by the sample application are part of Eclipse Microprofile specification, so we can use the java-microprofile Stack. We can initialise the development environment by issuing the following command within the finish directory:

appsody init java-microprofile none

There are 2 changes you will need to make in order for the app to run.

(1) Both the application and the java-microprofile Stack will include a pom.xml. The Stack’s will act as the parent, so before you can run the app you need to update the <parent> field in the application pom.xml, to reference the Stack as shown below:

<parent>
<groupId>dev.appsody</groupId>
<artifactId>java-microprofile</artifactId>
<version>0.2.1</version>
</parent>

(2) The Stack requires the application to either be a specific version of OpenLiberty like 19.0.0.5 or to rely on the default set in Stack by using ${version.openliberty-runtime}, so you’ll need to update <assemblyArtifact> appropriately.

Running the application

This will start a docker container and copy across the workspace and a controller. This controller will watch for changes in the src directory and issue mvn -Dmaven.repo.local=/mvn/repository compile to rebuild the app.

Once complete you can test the app by going to:
http://localhost:9080/LibertyProject/System/properties

You now have a fully functional development environment, where you can make changes in your IDE of choice, hit save, and refresh the above URL to see the changes take effect.

For example, try adding the following bold text within src/main/java/io/openliberty/guides/rest/PropertiesResource.java

@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonObject getProperties() {
JsonObjectBuilder builder = Json.createObjectBuilder(); builder.add("General Kenobi","Hello there"); System.getProperties()
.entrySet()
.stream()
.forEach(entry -> builder.add((String)entry
.getKey(),(String)entry
.getValue()));
return builder.build();
}

Build and deploy

Next Steps

Any questions or want to chat, come join us on Slack!

You can also find a bunch of other helpful guides for creating Java applications with OpenLiberty here.

Appsody

Appsody (https://appsody.dev)

David Harris

Written by

Manager for Appsody, Serverless, Node.js and Swift at IBM. Usually found dog walking, making music or asleep.

Appsody

Appsody

Appsody (https://appsody.dev) provides tools and capabilities to create, develop, build and deploy cloud-native applications.

David Harris

Written by

Manager for Appsody, Serverless, Node.js and Swift at IBM. Usually found dog walking, making music or asleep.

Appsody

Appsody

Appsody (https://appsody.dev) provides tools and capabilities to create, develop, build and deploy cloud-native applications.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store