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
#You can choose whatever you wish in the groupid and artifactid
GroupId: my.simplejavarestapi.com
ArtifactId: simplejavarestapi
Click Next
#Continue
#You can choose your own project name
Project name: simplejavarestapi
Finish
The project should open as the following:
In the pom.xml we need to add dependecies, under depencies lets add:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!-- Add sparkjava dependency, this is our REST routes framework --> <dependency> <groupId>com.sparkjava</groupId> <artifactId>spark-core</artifactId> <version>2.1</version> </dependency> <!-- GSON for generating JSON output --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> </dependency> <!-- Spark uses SLF4J for logging, so we need to a SLF4J binder to see log and error messages. --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.9</version> </dependency> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import com.google.gson.Gson; import spark.ResponseTransformer; public class JsonTransformer implements ResponseTransformer { private Gson gson = new Gson(); @Override public String render(Object model) { return gson.toJson(model); } } |
Lets create an pojo object that we will use in our code.
Create Greeting.java
1 2 3 4 5 6 7 8 9 |
public class Greeting { Â Â Â private int id; Â Â Â private String content; Â Â Â public Greeting(int id, String content) { Â Â Â Â Â Â Â this.id = id; Â Â Â Â Â Â Â this.content = content; Â Â Â } } |
In App.java lets add the lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import static spark.Spark.*; /** * Hello world! * */ public class App { public static void main( String[] args ) { //Spark uses filters to intercept any route, lets add a filter for "before" we need to register a Filter that sets the JSON Content-Type. before((request, response) -> response.type("application/json")); //Route to greeting get("/greeting", (request, response) -> new Greeting(1, "Hello, World!"), new JsonTransformer()); post("/greeting", (request, response) -> new Greeting(2, "Hello I'm Adding, " + request.queryParams("name")), new JsonTransformer()); put("/greeting", (request, response) -> new Greeting(3, "Hello I'm Updating, " + request.queryParams("name")), new JsonTransformer()); delete("/greeting", (request, response) -> new Greeting(4, "Hello I'm Deleting, " + request.queryParams("name")), new JsonTransformer()); } } |
Right click on App.java and click on Run ‘App.main()’
Open the browser and go to http://localhost:4567/greeting
GET: http://localhost:4567/greeting
1 2 3 4 |
{ "id": 1, "content": "Hello, World!" } |
POST: http://localhost:4567/greeting?name=Gabriel
1 2 3 4 |
{ "id": 2, "content": "Hello I'm Adding, Gabriel" } |
PUT: http://localhost:4567/greeting?name=Gabriel
1 2 3 4 |
{ "id": 3, "content": "Hello I'm Updating, Gabriel" } |
DELETE: http://localhost:4567/greeting?name=Gabriel
1 2 3 4 |
{ "id": 3, "content": "Hello I'm Updating, Gabriel" } |