My name is Diego Weidle Rost. I am a software development student trying to not get overwhelmed by tons of technologies that are available to web applications developers.
Currently, I am studying REST and Spring Boot, and I would like to share my discoveries with you through two blog posts, each one containing a video to help to share this information.
The first blog post (this one) will be more theoretical and touch more on what are these technologies and why they are so popular among web app developers.
The second post will be more practical and will cover how the two technologies work, and I will show you some examples and a simple guide on how to get it all working.
Each post will also have a video if you don’t feel like reading or just want a summary of the content. However, I suggest you read the post and watch the videos since sometimes one adds to the other. I added a section of the video to each corresponding section of this post.
I am not a regular writer or presenter, but I will try to explain the topic as best as I can with what I have learned so far. I found many complicated explanations out there and I want to make this easier for you to understand.
The focus of these posts and videos will be on REST. Spring Boot and Postman will be used to show you how the architecture works.
Before you keep going, here are some things that might help you have a better experience:
- Patience. I am not an instructor. Like I mentioned above, I am just a student like you who is still learning about REST and Spring. I just want to share with you what I learned so far. English is also not my primary language, so you might find a few (or many) grammar errors
- If you notice I wrote something wrong (grammatically or technically) please let me know so I can correct it and make this a better experience for future readers
- Some Java knowledge, especially web application features like Servlets and JSPs
- Understanding HTTP request and response will help to make it easier to understand REST
Before we get into REST, first let’s make sure you first understand what an API is in general.
API stands for “Application Programming (or Program) Interface”. APIs are everywhere. Many programs or websites use APIs to communicate with each other. An API sets the contract or the rules to how this communication will happen; what will be the format of the request and response. It also makes sure the application/client can only access what is needed from the other application/server.
Traversy Media, a very good YouTube channel (link below the image above and at the end of this post) does a very good analogy to help us understand what an API is: Imagine a restaurant. The customer making the order is the program doing the request, the client-side. The kitchen prepares the food ordered; it represents a program on the server side preparing the package the client requested. The waiter is the API. It takes the order from the customer/client and requests the kitchen/server to prepare the food/package in a certain way. Watch Traversy’s video for more details on this analogy.
I hope this simple analogy made it easier for you to understand what an API is. It is basically just a contract between two programs; like a real-life contract between individuals or companies.
What is REST API?
Now that the meaning of API is (hopefully) cleared up, we can start with REST. It stands for Representational (or Representation) State Transfer.
If you like web application development, you probably already have heard about REST (or RESTful). REST is an API architectural style that determines how an application will communicate with another application or a server. REST makes it easier to manage CRUD HTTP queries (Create, Retrieve, Update, Delete). It is commonly used in applications that require network communications or web applications and is used by most web application development companies. APIs made following REST standards are known as RESTful APIs.
You may have heard of MVC (Model View Controller). MVC is a design pattern also commonly used in many applications. In fact, REST and MVC are commonly used together in the industry. Rest is similar in the sense that it is also a kind of pattern. However, instead of defining the internal structure of the program, like MVC, it defines the program API, or how the program (a client), will communicate with the server.
For an application to be considered RESTful, it should follow a few standards:
- Clear separation of concerns: The client and the server clearly serve a single purpose and do not depend on each other. If you want to change something on one side, it will not affect the other side.
- Stateless: The client and the server do not need to know what state each other currently in. The server only needs to expose the minimum required to the client. The client can request something and continue with other tasks, without the need to wait for the response.
- Cacheable: Information can be “memorized” so if the same request is repeated, it can take the data from a local source for example. This way the client does not use resources from the server to make the same request. GET requests are cacheable by default but can be modified by adding some information to the request header. POST requests are not cacheable but can also be modified. PUT and DELETE requests cannot be made cacheable.
- Uniform Interface: All elements in the API should follow the same API contract when communicating between each layer
- Layered: The application communication is done through layers. Communication from a client must go through each layer before reaching the server making sure the package is following all the rules specified by the API
REST applications communicate with the server through URLs with hypermedia links so the application can create, read, update and delete objects in the server application.
A Hypermedia URL is a special kind of URL carrying resources in it. The URL contains the resource like a shopping cart containing all added items, i.e. /cart or a more specific URL pointing to just one item of the cart, i.e. /cart/1. The server will also send a hypermedia link back with the content requested.
Using these URLs, the application can perform the following operations:
- GET: Retrieve one or more resources from the server application
- POST: Create a resource in the server application
- PUT: Update a resource in the server application
- DELETE: Removes a resource from the server application
In the second part of this post, I will be providing more details on these operations with practical examples using Spring Boot and Postman and more details on the structure of a request.
I believe the main reason why REST is used by so many companies is that it is simple to implement, offers a clear separation of concerns, makes all operations more efficient and works with almost all languages (if not all) used in the industry nowadays.
It works so well with all these languages because it uses JSON inside the request and response. JSON is a very simple plain text file format containing objects (defined by curly braces) and arrays (square brackets) with data in a name/value pair format. Again, I will provide more details on requests and JSON in the next post.
What are Spring and Spring Boot?
Spring Boot is a framework based on Java. It is an extension of the already existing Spring framework. A framework is a kind of improvement made on top of an existing language, adding extra functionality to the original language. A framework can also be made for a specific function.
I will not get too much in-depth with Spring and Spring Boot because there is a lot to cover about it. Spring removes the need to add a lot of boilerplate code you would have to add while using regular Java when setting up a web application, helping you to be a much more productive developer.
The main objective of Spring is to make the development of web applications way easier, although Spring can also be used to make regular applications since it has all the stock Java features. I will be focusing more on the web and data part to demonstrate REST, but make sure to learn as much as you can about Spring. You can check the links in the sources section to learn more about Spring and Spring Boot.
Spring adds many features that will normally have to be added manually to a Java application. Among them are database communications, servlet features, security components, among others. It also uses a design pattern called Dependency Injection. A design pattern that injects one object into another, similar to the concept of composition. In this case, the object depends on another, since it cannot be instantiated without a previously instantiated object.
Spring Boot went even further by removing even more steps and lines of code required by Spring. The best feature added is probably the ability to add starter dependencies to the project, simplifying a lot of the initialization of a project. Spring Boot also includes an embedded server, so you don’t need to have Tomcat installed, making it easier to test your application saving you even more time. Lastly, Spring Boot also supports Thymeleaf (similar to JSTL but better), making it much easier to add templates to your web application and sharing REST responses with the user.
Other than the nice features added, these are some of the other differences between both:
- Fewer dependencies required to create a basic web application
- Simpler bootstrapping (the process of initializing the application)
- Better application deployment
Spring also added many new annotations to help with the development of the application and eliminating many lines of XML configuration lines. These are the available Spring annotations:
- Spring Core Annotations
- Spring Web Annotations
- Spring Scheduling Annotations
- Spring Data Annotations
- Spring Bean Annotations
We will be using some of these annotations in our examples in the next post, especially web and data annotations.
These annotations will be used to define things like Beans, dependency injection (which makes it easier to switch how requests are processed), and controllers (these eliminate most of the regular servlet code).
Spring Data is especially useful to communicate with eh database in web applications. You probably had to set up a connection to a database and wrote a bunch of code to be able to send and retrieve data from the database. This is what it looks like with Spring Data using a @Repository annotation:
One line of code and one annotation, for real. More on the second post.
In addition, and most important for RESTful apps, defines what methods are going to be used to make the HTTP requests, the request parameters, controllers, and mapping objects to databases, among others. We will cover these annotations in-depth and learn how to use them in our examples in the post following this one.
Postman is a small but powerful application to help backend developers in testing HTTP requests without the need to set up a webpage; without the need to set up the frontend.
You can do any operation you would normally do with a full app. All operations mentioned in the REST section can be tested. GET requests to check if resources from your app can be accessed, POST requests to check if your app is storing new data, PUT to test updates and DELETE to make sure data is being removed properly.
You only have to set up the app with a server (easily done with Spring) with support for the REST API. You can make unlimited requests locally with Postman installed in your machine or limited free remote requests through their website.
We will be setting up this environment and use Postman briefly to demonstrate all the requests mentioned earlier using our Spring REST API demo in the next post.
If you got to this point, you are probably a student like me. To be fair, in this industry, everyone is always a student, we must keep our skills up to date and need to be constantly researching and studying new technologies or updating what we already know about the technologies we already use.
I hope this post helped you in learning something new about REST and Spring or at least the post was enough to spark an interest in REST, Spring, and web applications in general. If you want to start developing your first web apps, Spring and REST will make everything much easier as they automate much of the work for you.
Like I mentioned a few times during the post I will be writing a second part to this post that goes more in-depth on how everything I mentioned above works in practice. If you liked this post be sure to continue to the next one. I will be updating this post with the link to the next post once it is ready in a few weeks.
Thank you for following along and see you at the next post!
- REST API Tutorial — https://restfulapi.net/
- Code Academy: What is REST? — https://www.codecademy.com/articles/what-is-rest
- Traversy Media: What Is A RESTful API? Explanation of REST & HTTP — https://www.youtube.com/watch?v=Q-BpqyOT3a8
- Wikipedia: Representational State Transfer — https://en.wikipedia.org/wiki/Representational_state_transfer
- Nordic APIs: CRUD vs REST: https://nordicapis.com/crud-vs-rest-whats-the-difference/
- Web Dev Simplified: What is REST? — https://www.youtube.com/watch?v=6sUbt-Qp6Pg
- Amigoscode: Spring Boot Tutorial For Beginners: https://youtu.be/r-6BwGW4Sr8
- Comparison between Spring and Spring Boot — https://www.baeldung.com/spring-vs-spring-boot
- Geek For Geeks — Stateless vs Stateful — https://www.geeksforgeeks.org/difference-between-stateless-and-stateful-protocol
- Journal Dev: Spring Annotations — https://www.journaldev.com/16966/spring-annotations
- Seobility: REST API — https://www.seobility.net/en/wiki/REST_API