Posts Developing Spark Java Api Endpoints using Annotations
Post
Cancel

Developing Spark Java Api Endpoints using Annotations

“Spark Framework is a simple and expressive Java/Kotlin web framework DSL built for rapid development.”

A quick look on sparkjava.com and you will see how easy it is to use, it literally takes a few minutes to get spark project up and running.

A sample example screenshot straight from the website :

spark

It looks so easy and tempting which I booted up my laptop and in a few clicks on IntelliJ Community Edition my “Hello World” (the above example) was up and running.

Next I went through some tutorials and that is when I started getting : “This will get ugly as the API expands feeling”.

Meaning: As you add more API end points to the above file and it will start to look cluttered, even if you move your logic to different class (which you should), still you will end up with a file full of endpoints.

Now I can totally see you thinking :

“Developers like him do not understand the concept of micro services, Micro services should be well… micro, with a few endpoints and each business functionality having its own automated build-deployment pipeline !.”

You are correct, I get that, please calm down !.

Even with say five endpoints, I don’t get the warm and fuzzes when I look at the “Endpoint File”, it feels unnecessary (at least for my usage).

I may be wrong but for me having the functionality to add @Get or @Post to the endpoint makes it easy to write/read.

So in this article I will explain how to add annotations to the above example.


So I created annotations first :

SparkApi annotation is a marker annotation that is to mark the class using it as an API class.

spark api

Source

  • Get Annotation is for HTTP Get calls.

get

Source

  • Post annotation is for HTTP Post calls.

post

Source

Here RetentionPolicy.RUNTIME is used because I need this annotation to be present at runtime so that I can use it to search for classes and for both Get and Post I have added methods Path and Accept Type, this is because I am going to call the below Spark methods.

get docs

Source

post docs

Source


Now the above annotations can be used in HelloWorld example.

hellwo world

Source

In above code at :

Line 9 : SparkApi annotation is used to mark this class as spark API class.

Line 19: Post annotation is used to mark method as responsible for handling HTTP post requests to the specified URL path and accept type.

Line 24: Get annotation is used to mark method as responsible for handling HTTP post requests to the specified URL path and accept type. This also shows how to handle query parameters.

Line 33: Get annotation is used to mark method as responsible for handling HTTP post requests to the specified URL with path parameter and accept type.

  • Now lets start the server and register all endpoints

server

Source

  • Now an external Reflections dependency is used, that will search for all classes with @SparkApi annotation.

register_endpoints

Source

  • Once all the classes have been found, next it will look for all methods with @Get and @Post annotations.

process_endpoints

Source

  • Then it will call spark Get/Post method to add the endpoint.

process_get_post

Source

  • Now last bit is to tell Spark what class and method to call when the path in “endpoint.path()” is called.

request_processor

Source

In above code at:

Line 23 : method object gets the class it is declared in.

Line 24 : HellowWorld class is overriding constructor with spark request, response, so here I am creating a new instance of the class using that constructor.

Line 25 : the method that we found using @Get/@Post annotation executes.

Every time endpoint loads the above code will be executed.

Which is why I do not like this piece of code because reflection is a bit slower compared to directly creating an instance of class.

So some performance testing will make me feel more comfortable I guess.


So here you go, that is it !, now when server starts it will call reflections dependency to:

  1. Search for all classes with @SparkApi annotation.
  2. Search for all methods in that class with @Get/@Post annotations.
  3. Add that method to Spark get/post method
  4. A new instance of that method will be called each time endpoint is hit.

Above Source Code:

GitHub Repository

References

Retention Policy Java Docs

Spark API Java Docs

Why is reflection slow

This post is licensed under CC BY 4.0 by the author.