Hi everyone,
As a mobile developer, one problem i faced a lot is server not ready
.
Alternates are available to build api-server, our goal is create light weight api-server, our main purpose is to create minimal usable server which we are not going to deploy anywhere.
So we will focus on Spark Java(http://sparkjava.com).
We will try to create a small twitter like api-server using spark java.
Lets get started:
We will use Intellij IDE for creating our api-server application(i still like full application
instead calling it as app
❤️ )
- File->New Project->Maven
- Create from archtype->maven-archtype-quickstart
- Fill in group-id and artifacts details then proceed
- Let other defaults be as it is
- Name project
After all these you will be presented with ready to use maven project,
add a run configuration using App class having main method and press play button app should print Hello World!
thats our maven project created.
Add some server code to it.
To use spark java we first need to add spark-java dependency to maven(pom.xml) file.
Lets move ahead to http://sparkjava.com/documentation
copy maven dependency and paste it inside dependencies tag in pom.xml
here is how it looks as of writing this article:
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.6.0</version><!-- check for the updated version from documentation -->
</dependency>
spark.Spark
class has all the necessary stuffs to get started with HelloWorld api.
add these lines to main method:
Spark.get("/sayhello", (request, response) -> {
return "Hello buddy!!!";
});
Save and run the application then launch http://localhost:4567/sayhello
on your browser, it should respond Hello buddy!!!
Congrats you just created a very tiny server-application.
Thats it for now, in the next section we will configure our application to behave more like an api-server intead of hello world.
Thank you for reading.
Top comments (0)