#1 My First Rest API With Spring Boot Freamwork

I hope you must be familiar with Java. Spring is a Java framework which is used for Rest api development by using Java as language. If you want to have basic idea about spring then you can visit here.

In this tutorial I will be focusing on simple rest api using Get method. Please follow steps below to create your first Rest api:

1. Create project schema:

While creating project schema, we can simply create via eclipse by going file -> new -> Maven Project

If we are using above method to create project schema then for Maven dependencies we will need to make entry in Pom.XML. To avoid that we can refer start.spring.io. In this tutorial I will be creating project via this interface only.

So lets avoid any more delay and start development in some very simple steps:

Open website start.spring.io and enter group (package), Artifact (name). After this add all required dependencies. Initially we will required only Spring MVC dependency as in screenshot below:




After selecting all information, Click on Generate Project. It will download Zip file of project schema. Extract it and open in your eclipse workspace as a project.

Once you will open it, Go to HelloWorldApplication.java file. This is main file of project, containing main method.



2. Build Maven:


Once project schema is set, now time to build Maven project.

Right click on project name and go to Run as -> Maven Build.




In build setting, configure as given below (enter clean install and mark skip tests):





Congratulation, You are now ready to go for your first API.




3. Write First API

So after all is set without any error, now its time to create first hello world api.

First create simple Java class ApiController.java in existing package.

Now open your ApiController.java and write code as given below:



I just written simple method which return String and add annotation @RequestMapping("/hello")@RequestMapping need to be written on top of every spring method which is used for api purpose. Api is named as hello ( passed in @Requestmapping)

Don't forget @RestController on top of class name. Spring will scan ApiController.java for api only if you mention @RestController on top of this. There are more annotations which we will be discussing in our upcoming blogs, for now lets focus on these only.

Once you completed all steps above, Its time to cheers because you have almost completed your first Spring API.

Now Right click on main method OR on class which containing main method, go to Run As -> Java Application




Once you run it, now go to browser and type URL as given below:



Congrats, time to cheers now but wait, Did you noticed that we don't require to add any tomcat from our end as we were doing before Spring boot. Spring boot application has its own in build tomcat instance which by default run at Port 8080.


I will be sharing more tutorials in this series, So be connected :)

If you like it or have any feedback then please comment on it.

Comments

Post a Comment

Popular posts from this blog

#2 Simple Spring Rest Api with JDBC connection

Swift Initialization