Spring is an open-source Java framework that is useful, among other things, for building RESTful web applications. Like any web framework, Spring comes with an established set of code conventions and patterns, such as predefined templates, databases, and functions. These generic code conventions provide reusable infrastructural support, enabling developers to easily build a variety of applications without reinventing the wheel at the start of every project. Spring focuses on project “scaffolding” so that developers can concentrate on the core logic of applications (you can think of Spring as a Node.js, Django, or Ruby on Rails but for Java!).
Indeed, Spring contains templates for many different kinds of applications, or “Spring Projects”, (including Spring Cloud, Spring Web Services, Spring Security, etc.). Spring Boot is one of the easiest ways to build Spring web projects, enabling applications to run with minimal configuration. Throughout this lesson we’ll be using Spring Boot to build our own web apps.
So how does a Spring web application actually work? The flow chart on the right shows the sequence of a typical HTTP GET request.
- The client sends a GET request to the Spring web server (i.e. the Spring application).
- The server sends a data request to the data store to retrieve the information requested by the client.
- The data store sends the requested data back to the server, if available.
- The server sends that data – the HTTP Response – back to the client and displays it on the browser.
It’s important to note that this is just one scenario of a browser-based client initiating a GET request to retrieve some information from the data store. Depending on the application scenario, we might not always want or need to interact with the data store. For example, in the case of DELETE, it’s possible that the data store won’t return any data to the server. Moreover, a client is not limited to being the browser; a client can also be a desktop or mobile app, or tools and scripts.
In the following exercises, we’ll explore the basic functionality of the Spring framework by building a web app that responds to two common types of HTTP request methods: GET and POST.
For the purposes of this lesson, try to focus on the overall functionality of the Spring application (i.e. the backend operations by which the Spring server/application responds to requests) and how to interact with it (i.e. how the user sends requests to the Spring server/application). Don’t worry about the exact syntax of the code: we’ll provide you with everything you need to copy-and-paste into the workspace.
Let’s get started!