REST and Spring Boot: Automating and Simplifying Web Development: Part 1

Diego Weidle Rost
11 min readJun 11, 2021
Section 1: Intro (0:00–1:00)

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

API

Section 2: API (01:00–2:52)

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.

From Traversy Media — https://www.youtube.com/watch?v=Q-BpqyOT3a8&t

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?

Section 3: REST API (2:52–7:16)

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.

Image Author: Seobility — https://www.seobility.net/en/wiki/REST_API

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?

Section 4: Spring Boot (7:16–10:31)

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.

https://start.spring.io/

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
Controller annotation and Autowired annotation

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:

A Repository interface declaration

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

Section 5: Postman (10:31–11:56)

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.

A GET request made in Postman

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.

Conclusion

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!

Sources

--

--

Diego Weidle Rost

I'm a passionate software developer always eager to learn about technology. Here I hope to also share something of what I learn with fellow researchers