Building an application with Helidon MP

In the last post, we have discussed building an application with Helidon SE. In this post, we will implement the same RESTful APIs, but use Helidon MP feature instead.

Kick start a Helidon application

Follow the steps of Helidon official doc, generating an application skeleton from Helidon archetypes will be done in seconds.

Generate project skeleton

Open your terminal, run the following command to generate our sample project from Helidon MP archetype.

After it is done, a new folder named quickstart-mp will be created in the current folder, which contains the skeleton codes of this project.

Build

In your terminal, switch to the newly created quickstart-se folder, run the following command to build the project.

When it is finished, you will see there is a jar quickstart-mp.jar generated in the target folder.

Run

Run the application via:

As you see, different from Helidon SE, a JBoss Weld SE container was bootstrapped in Helidon MP for dependency injection.

Let’s test the sample API using curl command. By default, the generated codes provide similar sample APIs as the Helidon SE one.

Explore the source codes

Import the source codes into your favorite IDE(Intellij IDEA, Eclipse, Apache NetBeans etc), expands all nodes, the project structure looks like this.

In the pom.xml file, it defines a maven-dependency-plugin plugin to copy all its dependencies into target/libs folder, and package this project into a single thin jar file by maven-jar-plugin. When you ran java -jar quickstart-se.jar, it will search classpaths in the libs folder.

maven-jar-plugin specifies io.helidon.examples.quickstart.mp.Main as main class, which is responsible for bootstrapping the application.

In the io.helidon.examples.quickstart.mp.Main file, there is a main method, which handles the following things:

  • Configure logging with jdk logging framework.
  • Create a Server instance via Server.create.
  • By default, it can pick up a microprofile-config.properties in the project classpath if it exists.
  • Then start the web server.

If you have some experience of JAX-RS, it is easy to understand the generated skeleton codes.

  • JaxrsActivator declares a JAX-RS application, it activates JAX-RS in your application, and as the entrance of JAX-RS facility.
  • GreetResource is a generic JAX-RS resource class.

Build your REST APIs

Next we will try to convert our former APIs to use Helidon MP.

Cook your first APIs

Let’s start with cooking the Post APIs, the expected APIs are listed below.

URI request response description /posts GET 200, [{id:’1', title:’title’}, {id:’2', title:’title 2'}] Get all posts /posts POST {title:’title’,content:’content’} 201, set new created entity url in Location header Create a new post /posts/{id} GET {id:’1', title:’title’,content:’content’} Get a post by id /posts/{id} PUT {title:’title’,content:’content’} 204, no content Update specific post by id /posts/{id} DELETE 204, no content Delete a post by id

Firstly, create a Post class which represents the post entries.

The of method provides a factory to create a new post quickly.

Create a dummy PostRepository for retrieving from and saving into database, currently I used a ConcurrentHashMap instead of a real world data storage.

Create a PostResource to handle Post requests.

The good part of JAX-RS is it is easy to append subresource in the current resource, such as CommentResource.

Create comment APIs

Imagine a user can comment on a certain post, we can add the following comment APIs.

Uri Http Method Request Response Description /posts/{id}/comments GET 200, [{‘id’:1, ‘content’:’comment content’},{}] Get all comments of the certain post /posts/{id}/comments POST {‘content’:’test content’} 201 Create a new comment of the certain post

Create a resource class for Comment.

The Comment and CommentRepository is similar to the former version. Please check our source codes for details.

Register it in the JaxrsActiviator class.

Run the application by run Main class in your IDE. Let’s have a try with our new created post APIs.

Test the APIs

Get all posts.

Get post by id.

Create a new Post.

The new created Post can be fetched by URL specified in the responseLocation header.

Verify if it is created successfully.

Delete a post by id.

Verify if the post is deleted.

Handle exceptions

Like generic JAX-RS support in a traditional Java EE application, we can define a custom ExceptionMapper to handle the custom exceptions.

In this example, define an exception named PostNotFoundException.

Create an ExceptionMapper for this PostNotFoundException.

@Provider can be recognised by JAX-RS container at runtime. When a post is not found, set response status as 404.

Do not forget to register it in the JaxrsActivator class.

NOTE: For those who are familiar with Java EE/JAX-RS, please remember to register these JAX-RS components in the JAX-RS activator class manually. In a standard Java EE application, JAX-RS registration is not a must, they can be scanned by container in the container startup stage.

Restart the application, use curl to check if the error handling is worked as expected.

Source Codes

Get the source codes from my github, and play it yourself.

Written by

Self-employed technical consultant, solution architect and full-stack developer

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