Rest API (Web Api) Part 1
Rest (REpresentational State Transfer) API (Application Program Interface) a lot of abbrevations, but what is a REST API and how is it related to Web Applications?
It's very unlikely that you have never met or interacted with a REST API before, when using different web applications/services. Take forexample Google Maps, when you enter a location and the Map service provide you with driving guidelines presented nicely within the Google Maps GUI(Graphical User Interface). Such task is completed using API's communicating with the Google Servers requesting the needed data. In that way you do never directly talk to the servers containing the actual information.
Lets start with REST, what is it? REST is an architectual style, which in most cases is based on HTTP protocols to communicate between different services/clients. It's in it's simple sense a set of constraints and properties used to define such communication. When HTTP is used the following operations are avalible:
- GET - Request data from a specified resource (Get relevant posts on your Facebook Wall)
- POST - Submits data to be processed (e.g save a user in a Database)
- PUT - For saving or updating a resource on the server
- DELETE - For deleting a given resource
With the basic understanding of how API's are used and what operations are available over HTTP, it's time to look into how such can be implemented. Within large web applications we will often need several API's in order to process and offer different services like e.g creating users, saving posts and editing of profile information. When web API's are combined we often refere to them as a Mashup.
A Mashup in web development is a web application that uses content from more than one source to create a single new service displayed in a single graphical interface. 1.
It's therefore important to be aware of how we deploy different API's based on how they are to be used. To do so, we refere to release polices. The following release polices are normaly used:
- Private: The API is for internal company use only
- Partner: Only specific business partners can use the API
- Public: The API is available for use by the public
With this short introduction to the different aspects of REST API's it's time to show how such can be developed and deployed. For this we will create a Public available API, in the programming language Java, running on a localhost environment.
For the implementation Spring Boot will be used, which is a popular Java framework for building web and enterprise applications. For more info about Spring Boot Click Here. To start up our REST API project navigate to http://start.spring.io, here the packages and version for our project will be set. To ensure you have the right setup you settings should look similar to the following picture.
Once your setup is similar to the picture above, click Generate Project. The Group/Artifact can however be named as you wish, to match your project. The next step is to open the project within an IDE. When opened for the first time, the IDE will most likely load for a few secs/min in order to setup dependencies. To ensure that you setup is right and that your application is able to compile and execute, run the project as a Java application within your IDE. If the build succeeded you will see something similar to this in the console:
Tomcat started on port(s): 8080 (http) with context path ''
Started RestApiDemoApplication in 3.332 seconds (JVM running for 5.3)
With that being tested it's time to get into the code. Navigate to src/main/java/yourPackageName/yourAtifactNameApplication.java. Here we will create our first Controller, which is a normal .java file with certain specifications within the code. As an example we will create a GET method which returns a String. We will do the following steps.
- Create a java class, e.g HelloWorldController.Java
- Define your class as a Controller in order to specify that our class needs to handle REST Requests using @Controller
- Create a method which returns a string e.g helloWorld
- Define the request type by using @RequestMapping and the path in which we will access it over HTTP e.g "/Hello".
Once completed, restart your application and you should now be able to navigate to localhost:8080/hello and see the following.
We have now created our first REST API with a GET method, in part 2 we will dive into POST, PUT and delete.
To secure your REST API please see Part 2 of this tutorial:
https://vinsloev.blogspot.com/2019/10/rest-api-web-api-part-2-security.html
Kommentarer
Send en kommentar