Spring — Your RESTful companion
While not inherently RESTful, the Spring Framework provides extensive support for building RESTful web services. At the heart of this process is Spring MVC, also known as Spring Web MVC which allows you to build web applications, RESTful or not.
Vocabulary Check 📖
A framework is a technological tool that provides components of completed work for a certain step in the development process, to help programmers with faster and more efficient software development. It is also a type of container.
A container is anything that packages code for deployment.
API stands for Application Interface, which is just anything we use to interface with an application, most commonly a website interface.
A controller is a class in which you would define how a request gets handled.
A servlet is a Java class that is used to handle incoming requests and generate dynamic responses for web applications. It is part of the Java Servlet API and provides a way to handle HTTP requests and responses. Though they operate much like controllers, servlets are low-level components that require developers to manage various aspects of request processing and response generation. They manually handle tasks like parsing incoming data, generating HTML content, managing sessions, and more.
A web server is a software application or program that serves as the foundation for delivering web content over the Internet. It handles incoming requests from clients (usually web browsers) and responds by sending back the requested web pages, files, or data.
The model is the object that is being transferred to or from the client-server.
The view is a component of MVC architecture useful for returning the user interface output to the user in response to the user request.
When seeking an efficient approach to web application development, one amazing option you should know about is the Spring Framework. Spring is a popular modern technology made exclusively for the Java platform. It is inherently RESTful which is good for those who want to build web services or APIs after the REST (Representational State Transfer) design pattern.
Why Spring?
Spring Web MVC
What Is Spring Boot?
What Is REST?
Why Spring?
When J2EE (Java 2 Platform, Enterprise Edition) was released by Sun Microsystems (now owned by Oracle), it provided a set of technologies, specifications, and APIs for large-scale, enterprise-level applications. We were introduced to Servlets, JavaServer Pages (JSP), Java Persistence API (JPA), Enterprise JavaBeans (EJB), Java Message Service (JMS), and more.
However, the real task became implementing all of these libraries and features manually before you could start to build your project. But all of that changed when Spring came along! Released in June 2003 by Rod Johnson under the Apache 2.0 license, what Spring does is it gathers these libraries for you, and will even retrieve the latest versions of them, no hassle. Some core functionalities of the Spring Framework include Dependency Injection (DI) and Inversion of Control (IoC).
Dependency Injection is a programming technique where the framework provides only the necessary constructors, methods, or objects to a function or object.
By using a framework in your system, you are not only cutting time in half with dependency injection, but you are also following the Inversion of Control (IoC) Principle. The term "Inversion of Control" can initially seem a bit counterintuitive, but it refers to a change in the traditional way of managing the flow of data control in a software system.
In traditional programming, the main program or component holds the control and manages the creation and interaction of objects in a system. This is considered the "normal" or "default" way of control.
There are five core modules in the Spring Framework:
- Spring Core: Includes core, beans, context, and expression language modules, providing the fundamental parts of the framework.
- Spring AOP:Provides aspect-oriented programming features.
- Spring Web MVC: Model-View-Controller implementation for creating web applications.
- Spring DAO:Offers JDBC and ORM support.
- Spring Web Module: Includes Web, Web-Servlet, Web-Struts, and Web-Portlet for web-based applications.
Resource for more details: Introduction to Spring Framework
All of these can be implemented singularly, but what if you wanted to incorporate all modules to use at your whim without downloading each one by one or having to make sure that they are the correct versions? That's why you need Spring Boot. While Spring Boot is considered a module in the Spring Framework, it is not a core module. It's purpose is to package and star up all modules in your pom.x file.
Spring Web MVC
Spring MVC plays a central role in developing RESTful web services. In the Web layer of the Spring Framework, we find inside the Servlet module Spring’s model-view-controller implementation for web applications. MVC stands for Model-View-Controller which references the design pattern Spring MVC follows which encourages the separation of the user interface (View) commands from the application code controllers which work at a higher level than servlets to delegate how requests should be processed.
What Is Spring Boot?
Spring Boot is not only an extension of the Spring Framework, it is in itself a framework for providing a set of conventions, templates, and tools that make it easier to set up and develop applications by reducing the need for manual configuration and boilerplate code. Because Spring Boot also wraps Spring MVC, it too follows the model-view-controller principle. Spring Boot is not a core module of the Spring Framework. Instead, it is a separate project in the wider Spring ecosystem. Spring Boot is built on top of the Spring Framework and provides a simplified approach for building and deploying Spring applications. It offers a range of features such as auto-configuration, standalone deployment, and opinionated 'starter' dependencies to simplify your Maven or Gradle configuration.
What is REST?
The REST (Representational State Transfer) design pattern is an architectural style used for web development. It defines a set of constraints and principles for creating web services that are scalable, simple, and stateless which leverage standard HTTP methods. Here's an overview of its key aspects:
- Resource-Based: In REST, everything is a resource, meaning it should be thought of as any content or data that can be accessed and manipulated via a unique URI (Uniform Resource Identifier). A resource can be a document, a photo, a service, etc.
- Statelessness: Each HTTP request from a client to server must contain all the information needed to understand and complete the request. The server does not store any state about the client session on the server side. This makes the REST services independent of the state of the client and easier to scale.
- Client-Server Architecture: REST follows a client-server architecture, where clients and servers act independently. The client sends a request, and the server returns a response. This separation of concerns supports the scalability and portability of applications.
- Uniform Interface: REST applications use a standardized interface, which simplifies and decouples the architecture, allowing each part to evolve independently. The four key principles of a uniform interface are:
- Resource Identification in Requests: Individual resources are identified in requests, for example using URIs in web-based REST systems.
- Resource Manipulation through Representations: When a client holds a representation of a resource, including any metadata attached, it has enough information to modify or delete the resource.
- Self-descriptive Messages: Each message includes enough information to describe how to process the message.
- Hypermedia as the Engine of Application State (HATEOAS): Clients interact with a REST API entirely through hypermedia provided dynamically by application servers.
- Use of HTTP Methods: REST APIs use standard HTTP methods in a meaningful way (GET for retrieving resources, POST for creating new resources, PUT for updating resources, DELETE for deleting resources).
- Stateless Communication: Each request from a client to a server must contain all of the information needed to complete the request. The server should not store anything about the latest HTTP request the client made. It should treat every request as new.
- Layered System: REST allows the use of a layered system architecture where each layer cannot "see" beyond the immediate layer with which they are interacting.
- Code on Demand (optional): Servers can temporarily extend or customize the functionality of a client by transferring executable code.
While not inherently RESTful, the Spring Framework provides extensive support for building RESTful web services. At the heart of this process is Spring MVC, also known as Spring Web MVC which allows you to build web applications, RESTful or not. Spring Boot is a useful when setting up all the requisite files for web development, still following the Model-View-Controller design pattern. Many other modules in the Spring Framework can be used to your advantage during development beyond the five that were mentioned. However, having a there are the most used when it comes to Spring.