Build Restful service with Java 8 in Five minutes

This is a very basic tutorial for beginners who would like to experiment with Java 8 and sparkjava.com Spark Framework.

Sometimes we can complex issues in our head more then it is, I was just trying to teach myself a way to make a simple Java Restful Api to handle few open and simple calls for me. I will share with you my start point.

We will use Intellij and create simple RestApi, basically if you have eclipse it is the same idea.


First lets create the project.
Open Intellij => Choose Maven project
Choose maven-archetype-quickstart
Click Next
intellij-maven

#You can choose whatever you wish in the groupid and artifactid
GroupId: my.simplejavarestapi.com
ArtifactId: simplejavarestapi
Click Next
intellij-maven-artifectid
#Continue

intellij-maven-setup

#You can choose your own project name
Project name: simplejavarestapi

intellij-maven-project

Finish

The project should open as the following:

start-pomxml

In the pom.xml we need to add dependecies, under depencies lets add:

We are done with our configuration, lets start with adding our code.

Spark uses an interface called ResponseTransformer to convert objects returned by routes to an actual HTTP response.this transfomer will route transforming output to JSON using Gson:

Create JsonTransformer.java

Lets create an pojo object that we will use in our code.
Create Greeting.java

In App.java lets add the lines.

Right click on App.java and click on Run ‘App.main()’
appmain

Open the browser and go to http://localhost:4567/greeting

GET: http://localhost:4567/greeting

 

POST: http://localhost:4567/greeting?name=Gabriel

 

PUT: http://localhost:4567/greeting?name=Gabriel

 

DELETE: http://localhost:4567/greeting?name=Gabriel

githubbutton

Download project from Github: https://github.com/gabriel-a/simplejavarestapi

Build Restful service with Java 8 in Five minutes
Gabriel Ajabahian
Tech lead / Senior Software Engineer at RIPE NCC
As a Software Engineer at RIPE NCC, I’m passionate about creating dynamic applications that is used around the globe.

I have extensive software engineering experience, recently I focus on Scala/Java and JavaScript. On the other hand, I have experience with C# and .net framework. I focus on integrating applications that are built against different frameworks and can run on different platforms.

Scalability, efficiency and performance are key topics for any software, that’s why I keep track of the newest technologies, I never underestimate the power of the cloud, that’s why I do believe in using this power with “edge applications” to expand software’s and solutions to a new level.

Being a software engineer who understand the business as well as my team, looking at things from different perspectives. Being open and honest and investing in relationship makes a big difference.

Specialties: Software development, Scrum/Agile, Process and Problem management.

PS: Please don’t contact me or connect with me if your recruiter, I’m not interested in any job.