Released in October 2002 as an open source framework and inversion of control (IoC) container developed using Java, Spring was built for the Java platform. It has transformed from a small collection of libraries into a huge collection of full-blown projects designed to simplify development even when the solution is complex.
This book journeys from a classic web application packaged as a jar and deployed to an application server to an application composed of a set of microservices that are easily deployed in a cloud environment, each of them on its own VM or container.
It all starts with a set of tools that need to be installed before the developer can write and run code.
If you know how to use SDKMAN,1 you can skip the next two sections that explain how to install the Java SDK and Gradle. If you do not know how to use SDKMAN or never knew it existed, give it a try; it is a tool for managing parallel versions of multiple SDKs. If you have other projects using different versions of Java and Gradle locally, this tool helps you switch between them without a fuss.
Install the Java SDK
We recommend that you set the JAVA_HOME environment variable to point to the directory where Java 14 was installed (the directory in which the JDK was unpacked) and add $JAVA_HOME/bin (%JAVA_HOME%in for Windows users) to the general path of the system. The reason behind this is to ensure any other development application written in Java use this version of Java and prevent strange incompatibility errors during development. If you want to run the build from a terminal, you are certain that the expected version of Java is used.
Install Gradle
The sources attached to this book can be compiled and executed using the Gradle Wrapper, which is a batch script on Windows and a shell script on other operating systems.
When you start a Gradle build via the wrapper, Gradle is automatically downloaded inside your project to run the build; thus, you do not need to explicitly install it on your system. The recommended editor for development introduced next knows how to build code using Gradle Wrapper. Instructions on how to use the Gradle Wrapper are available in the public documentation at www.gradle.org/docs/current/userguide/gradle_wrapper.html.
A recommended practice is to keep the code and build tools separately. If you decide to install Gradle on your system, you can download the binaries from www.gradle.org, unpack it and copy the contents to the hard drive. (Or, if you are curious, you can download the full package containing binaries, sources, and documentation.) Create a GRADLE_HOME environment variable and point it to the location where you unpacked Gradle. Also, add $GRADLE_HOME/bin (%GRADLE_HOME%in for Windows users) to the system’s general path so that you can build the project in a terminal.
Gradle was chosen as a build tool for this book’s sources because of the easy setup, small configuration files, flexibility in defining execution tasks, and the Spring team currently uses it to build all Spring projects.
Running this command also verifies that Gradle is using the intended JDK version.
Install Apache Tomcat
The Spring MVC projects of this book were tested in Apache Tomcat 9.x. To install Apache Tomcat, go to the official site and get the version matching your operating system. Unpack it in a familiar location. On Unix-based systems, you might be able to install it using a package manager. If you install it manually, remember to go to the bin directory and make all files executable.
Recommended IDE
IntelliJ IDEA offers outstanding framework-specific coding assistance and productivity-boosting features for Java EE and Spring also includes very good support for Gradle. It is the perfect choice to help you focus on learning Spring (and not how to use an IDE). It can be downloaded from the JetBrains official site (www.jetbrains.com/idea/). It is also light on your operating system and easy to use.
IntelliJ IDEA also integrates well with Apache Tomcat, which allows you to deploy your projects to start and stop the server from the editor.
And now that the tools have been discussed, let’s talk about the project.
The Bookstore Project
Bookstore Project Modules
Chapter | Project Name | Description |
---|---|---|
– | bookstore-mvc-shared | Entity and utility classes used by Spring MVC projects |
– | bookstore-shared | Entity and utility classes used by Spring Boot projects |
1 | chapter1-bookstore | A simple Spring Boot Web MVC project with typical web structure (static resources in the webapp directory) |
1 | chapter1-mvc-bookstore | A simple Spring MVC project. |
2 | chapter2-bookstore | A simple Spring Boot Web MVC project with typical Boot structure (static resources in the resources/static directory) |
2 | chapter2-sample | A simple project with non-web samples for Chapter 2 |
5 | chapter5-bookstore | The Bookstore Spring Boot MVC project, using Thymeleaf views |
6 | chapter6-bookstore | The Bookstore Spring Boot MVC project, using Apache Tiles views |
7 | chapter7-bookstore | The Bookstore Spring Boot MVC project with support for upload files |
8 | chapter8-bookstore | The Bookstore Spring Boot MVC project with support for various view types |
9 | chapter9-1-bookstore-no-boot | The Bookstore Spring WebFlux project deployed on Apache Tomcat (uses reactive controllers) |
9 | chapter9-2-bookstore | The Bookstore Spring Boot WebFlux project (uses reactive controllers) |
9 | chapter9-3-bookstore | The Bookstore Spring Boot WebFlux project (uses functional endpoints) |
10 | chapter10-1-bookstore | The Bookstore Spring Boot WebFlux project supporting case-insensitive URIs and internationalization via a web filter (the most elegant solution) |
10 | chapter10-2-bookstore | The Bookstore Spring Boot WebFlux project supporting validation |
10 | chapter10-3-bookstore | The Bookstore Spring Boot WebFlux project supporting case-insensitive URIs and internationalization via LocaleContextResolver |
11 | chapter11-1-bookstore | The Bookstore Spring Boot MVC project with WebSocket chat |
11 | chapter11-2-bookstore | The Bookstore Spring Boot WebFlux project with Tech News emitted by a reactive stream over WebSocket |
11 | chapter11-3-client-bookstore | RSocket client project |
11 | chapter11-3-server-bookstore | RSocket server project |
11 | chapter11-4-server-bookstore | The Bookstore Spring Boot WebFlux project using reactive security |
12 | chapter12-bookstore | The Bookstore Spring Boot MVC project using Spring Security |
12 | chapter12-mvc-bookstore | The Bookstore Spring MVC project using Spring Security |
13 | chapter13-account-service | Microservice providing the Reactive Account API |
13 | chapter13-book-service | Microservice providing the Reactive Book API |
13 | chapter13-discovery-service | Microservice discovering and registering the other microservices |
13 | chapter13-newreleases-service | Microservice providing a single reactive endpoint emitting random Book instances |
13 | chapter13-presentation-service | Microservice with a Thymeleaf web interface interacting with the other interface |
13 | chapter13-technews-service | Microservice providing a single reactive endpoint emitting random String instances representing tech news |
Projects with names that contain -mvc- and chapter9-1-bookstore-no-boot are compiled and packed as a *.war can be run in Apache Tomcat. Except for chapter2-sample, all the other projects are built using Spring Boot and can be run by executing their main class. The chapter2-sample project has multiple main classes that you can run to test a specific scenario.
Building the Project
Once you’ve installed the recommended tools, the next step is getting the project sources from GitHub.
The GitHub project page is at https://github.com/Apress/spring-mvc-and-webflux.
You can download the repo page sources, clone the project using IntelliJ IDEA, or clone it using Git in the terminal. You can use HTTPS or Git protocol—whatever feels familiar and easy.
Building the Project for This Book
Once the project builds in the terminal, you can verify that you have the right project and the right tools. It is now time to open it in IntelliJ IDEA.
- 1.First, if you want to use Gradle Wrapper, skip this step. Otherwise, go to the Gradle view, click the little wrench button (the one labeled Build Tool Settings), and a window appears to allow you to choose the Gradle version. If you have Gradle installed on your system, and the GRADLE_HOME environment variable is set up, IntelliJ IDEA finds it. Still, it does not use it if the project contains a Gradle Wrapper configuration. To use Gradle on your system, choose Specified location in the section of the window marked in Figure 1-6.
And, while you’re at it, make sure the Gradle JVM is set to JDK 14 as well.
- 2.In the IntelliJ IDEA main menu, select File > Project structure…. The Project Structure window allows you to configure the project SDK and the project language level. Make sure it is JDK 14 for both, as depicted in Figure 1-7.
Running the Projects
- 1.
Click the list of project launchers in the upper-right corner.
- 2.
Select Edit Configurations….
- 3.
In the Edit Configurations window, select the type of launcher that you want to create.
- 4.In the upper-left corner, click the + button. In the list of launcher types, select Tomcat Server > Local (see Figure 1-9).
- 5.
In the Run/Debug Configurations window, a form needs to be populated with the location of the Apache server and the project to deploy. First, name the configuration. Choose a name related to your project.
- 6.
Click the Configure button.
- 7.
Select your Apache Tomcat server directory.
- 8.
Click the OK button.
- 9.Click the Fix button. You are warned that you must select something to deploy (see Figure 1-10).
- 10.
In the list, select the project that you want to deploy.
- 11.
Next, in the Deployment tab, you can edit the context path because the autogenerated one is weird.
- 12.Click the OK button, and you are done (see Figure 1-11).
Now, the name of your launcher appears in the list mentioned in the first step. You can start Apache Tomcat by clicking the Run button (the green triangle next to the Launcher list). If all goes well, IntelliJ opens a browser tab to the main page of the project.
Summary
Hopefully, the instructions in this chapter are enough to help you get started. If anything is missing or unclear, feel free to first ask Google. If that does not work, create an issue on GitHub.
Happy coding!