Exploring the World of Web Development Part 4 - Write Rest APIs
One of the main advantages of writing a back-end Application using Spring is that Spring has the ability to make a dependency injection, make it modular and loose coupling, your code easily reusable and can be shared across different components or modules. Each class in Spring is called a component that promotes the Single Responsibility Principle. Each component in spring can be any type of Configuration, Controller, Model, Service, Repository, etc.
A. Step by step develop Rest API using Spring
Without further ado, we are now trying to develop our own first API, to supply a list of products and handle a contact form validation and storing the contact data, and then displaying the list of user submitted feedback. At this moment we are not gonna store any of the data in a Database server, we will store the data in Memory. We will learn about SQL database integration using Spring in the next tutorial.
First thing to do to create your Spring skeleton, you can download the Spring project skeleton using an online feature visit http://start.spring.io.
- Select Maven as Project type, Spring boot 2.7.x and Java 11 as the language
- Choose the following dependencies
After you finish naming your project, then extract the zip file that contains the empty spring project skeleton. Open the folder using Intellij IDEA.
In the most common structure of Spring Application is consist of five different type of Components:
1. Configuration
Here’s an example of Spring configuration to allow CORS on certain web domains.
2. Controller
And we are beginning to create A rest API with an endpoint named “/products”.
3. Service
Looking at the above controller, we call a service to get all of the products, and here we write our first service using Spring.
4. Model
And finally we have to create a Model and Repository for object representation of a Product.
We use a Java library called lombok, so we don’t have to create Constructors, getters, and setters. Those will be automatically generated by Lombok.
5. Repository
A repository is a type of component that provides a higher-level interface. By using a JPA repository, you don’t have to implement a Query to database. Spring provides abstraction for save(), findById(), findAll(), deleteById(), etc.
Finally, you may need to test out your first Rest API application. If you are using Intellij you can press the run button, but if you want to run your Spring app on a terminal, you can do the following:
cd path/to/your-project
mvn spring-boot:run
And then using either Postman or open it on the Browser, open the http://localhost:8080/products.
B. Spring is very suitable for large-scale projects
In my web development career writing backend applications, like 99% I would only use Java and PHP. The two languages are widely used for the development of almost all of the REST API that I have been developing. On the Java side Spring is the framework of choice for almost all of my Rest API. Before that I used PHP alot, I used that CodeIgniter-3 framework. I have been using Symfony and Laravel, but only for very small projects, but a long time ago I have not been doing any PHP code anymore, so this tutorial will be conducted using Java and Spring framework.
For me, both Java and PHP have their own strengths and weaknesses, for projects that are not very complicated and if you want a simpler language to do simple things quicker, you can write your Rest API using PHP. And what you care about is how to finish your project quicker, optimisation is not your priority, you will do it later as you finally reach a maximum user your app can handle. IF you want to write a quick and simpler Rest API. Still you have plenty of options : Firebase, NodeJs, Go and Python. They have a similarity which is to write code in no time.
On the other hand Java is a popular choice for building robust and scalable REST APIs. Especially using Spring Framework, the spring offers a rich ecosystem which provides extensive support for API development. Because of the strong typing and strong object-oriented styles, it makes Java code much more maintainable, easy refactoring and has better support for modern IDE. When you talk about IDE, I recommend anyone to develop their code using Intellij IDEA, it is a very awesome tool for various software development, Java is the one that has major support for it.
I like the Spring framework although it is kind of not simple because there’s so many things to know, basically there are hundreds of Spring Classes that you have to remember. But it helps me simplify the development process and promotes good software engineering practices. Like I have described, you have to create the right structure for your project, because when your project grows, you create lots of Components that you have to remember where it is and what it is doing.
Something that I thought of when it comes to the Core of Spring framework is the IoC container, this thing is the underlying mechanism that manages the creation and lifecycle of your components. And some other thing that is the joy of writing your back-end app with Spring is the easy data access using Spring Data, like example above with the Repository component, I basically don’t write anything to access and alter the database. Spring uses ORM and frameworks like Hibernate, not only for SQL based databases, it even supports NoSQL databases like MongoDB.
I also like how easy it is to configure your app security. Spring Security is a Spring module that helps in guarding your application by providing security features like authentication for accessing certain API. We don’t have to worry about SQL injection or any kind of security threat to your application.
C. The full source code
Thank you for keeping up until this moment, now I am glad to share the full source code of the project we created above.
- Github link simple Rest API using Spring: mudiadamz/backend-department-store
- The previous chapter of Web development series: Tutorial Part 3 about Javascript
- The next chapter Part 5 - Spring Data and MySQL
Stay tuned for the next tutorial about Database integration in Spring.