© Marten Deinum and Iuliana Cosmina 2021
M. Deinum, I. CosminaPro Spring MVC with WebFluxhttps://doi.org/10.1007/978-1-4842-5666-4_1

1. Setting up a Local Development Environment

Marten Deinum1   and Iuliana Cosmina2
(1)
MEPPEL, Drenthe, The Netherlands
(2)
EDINBURGH, UK
 

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.

../images/300017_2_En_1_Chapter/300017_2_En_1_Figa_HTML.jpg 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

Since Spring is a Java framework to write and run Spring applications, you need to install the Java SDK. This project was written and built with JDK 14. To install JDK 14, download the JDK matching your operating system from www.oracle.com/java/ and install it. So, if you are building an application and intend to use it for financial gain, you might want to look at Oracle licensing or use an open source JDK.2
../images/300017_2_En_1_Chapter/300017_2_En_1_Fig1_HTML.png
Figure 1-1

The Java logo3

../images/300017_2_En_1_Chapter/300017_2_En_1_Figb_HTML.jpg 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.

Restart the terminal and verify that the version of Java the operating system sees is the one that you installed by opening a terminal (the Command prompt in Windows or any type of terminal installed on macOS and Linux) and type the following.
> java -version  # to check the runtime
And then the following.
> javac -version # to check the compiler
You should see an output similar to the following.
>  java -version
java version "14.0.2" 2020-07-14
Java(TM) SE Runtime Environment (build 14.0.2+12-46)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)
>  javac -version
javac 14.0.2

Install Gradle

Gradle is an open source build automation tool designed to be flexible enough to build almost any type of software. It uses Groovy in its configuration files, which makes it customizable. The project attached to this book was successfully built with Gradle 6.x.
../images/300017_2_En_1_Chapter/300017_2_En_1_Fig2_HTML.jpg
Figure 1-2

The Gradle logo4

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.

To verify that the operating system sees the Gradle version that you just installed, open a terminal (the Command prompt in Windows, and any type of terminal installed on macOS and Linux) and type
gradle -version
You should see something similar to the following.
gradle -version
------------------------------------------------------------
Gradle 6.7
------------------------------------------------------------
Build time:   2020-08-04 22:01:06 UTC
Revision:     00a2948da9ea69c523b6b094331593e6be6d92bc
Kotlin:       1.3.72
Groovy:       2.5.12
Ant:          Apache Ant(TM) version 1.10.8 compiled on May 10 2020
JVM:          14.0.2 (Oracle Corporation 14.0.2+12-46)
OS:           Mac OS X 10.15.6 x86_64

Running this command also verifies that Gradle is using the intended JDK version.

Install Apache Tomcat

Web applications are meant to be hosted by an application server unless they are built using Spring Boot, in which case it is more practical to rely on an embedded server. Apache Tomcat 5 is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language, and WebSocket technologies.
../images/300017_2_En_1_Chapter/300017_2_En_1_Fig3_HTML.jpg
Figure 1-3

The Apache Tomcat logo6

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

The IDE that we recommend you use with the code in this book is IntelliJ IDEA. It is the most intelligent Java IDE.
../images/300017_2_En_1_Chapter/300017_2_En_1_Fig4_HTML.jpg
Figure 1-4

The IntelliJ IDEA logo7

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

The project containing the sources for this book is organized as a multi-module Gradle Project. Each chapter has one or more corresponding projects that you can easily recognize because they are prefixed with the chapter number. Table 1-1 lists these projects and provides a short description for each.
Table 1-1

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.

../images/300017_2_En_1_Chapter/300017_2_En_1_Figc_HTML.jpgThe 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.

You can build the project using IntelliJ IDEA, but if you are opening it for the first time, it takes a while to figure out the project structure and index the files. We recommend that you open a terminal and build the project by executing the command in Listing 1-1. The output should be similar to that one, and it must certainly contain BUILD SUCCESSFUL.
> gradle clean build
...
BUILD SUCCESSFUL in 3m 1s
150 actionable tasks: 148 executed, 2 up-to-date
Listing 1-1

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.

The first thing you notice is that IntelliJ IDEA is trying to decide the Gradle and the JDK versions. And it doesn’t always work, especially if you have multiple versions of each on your system. In the right corner, you might see notifications like the one in Figure 1-5.
../images/300017_2_En_1_Chapter/300017_2_En_1_Fig5_HTML.jpg
Figure 1-5

IntelliJ IDEA trying to infer Gradle and JDK version

To fix that, you must do the following.
  1. 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.
    ../images/300017_2_En_1_Chapter/300017_2_En_1_Fig6_HTML.jpg
    Figure 1-6

    IntelliJ IDEA Gradle and Gradle JVM setup

    And, while you’re at it, make sure the Gradle JVM is set to JDK 14 as well.

     
  1. 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.
    ../images/300017_2_En_1_Chapter/300017_2_En_1_Fig7_HTML.jpg
    Figure 1-7

    IntelliJ IDEA Project JDK setup

     
If all goes well, IntelliJ IDEA uses Gradle and JDK to build your project and execute tests. If you want to build your project in IntelliJ IDEA, use the Gradle View. When the project is loaded correctly, all modules should be listed together with a set of Gradle tasks grouped by their purpose. Under the build group, a task named build is the equivalent of the Gradle command in Listing 1-1. Figure 1-8 shows a successful Gradle build run in IntelliJ IDEA.
../images/300017_2_En_1_Chapter/300017_2_En_1_Fig8_HTML.png
Figure 1-8

IntelliJ IDEA successful Gradle build

Running the Projects

The projects that are not built using Spring Boot need to be deployed to an Apache Tomcat server. After a successful Gradle build, the artifacts should be already generated for all projects. To deploy your project on your local Apache server, you must do the following.
  1. 1.

    Click the list of project launchers in the upper-right corner.

     
  2. 2.

    Select Edit Configurations…​.

     
  3. 3.

    In the Edit Configurations window, select the type of launcher that you want to create.

     
  4. 4.
    In the upper-left corner, click the + button. In the list of launcher types, select Tomcat Server > Local (see Figure 1-9).
    ../images/300017_2_En_1_Chapter/300017_2_En_1_Fig9_HTML.jpg
    Figure 1-9

    IntelliJ IDEA Launcher options

     
  1. 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.

     
  2. 6.

    Click the Configure button.

     
  3. 7.

    Select your Apache Tomcat server directory.

     
  4. 8.

    Click the OK button.

     
  5. 9.
    Click the Fix button. You are warned that you must select something to deploy (see Figure 1-10).
    ../images/300017_2_En_1_Chapter/300017_2_En_1_Fig10_HTML.jpg
    Figure 1-10

    IntelliJ IDEA Launcher options for configuring Apache Tomcat server and artifact to deploy

     
  1. 10.

    In the list, select the project that you want to deploy.

     
  2. 11.

    Next, in the Deployment tab, you can edit the context path because the autogenerated one is weird.

     
  3. 12.
    Click the OK button, and you are done (see Figure 1-11).
    ../images/300017_2_En_1_Chapter/300017_2_En_1_Fig11_HTML.jpg
    Figure 1-11

    IntelliJ IDEA Launcher options to configure artifact to deploy

     

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.

The log console of the Apache Tomcat in IntelliJ IDEA can give you more information in case the deployment failed. Figure 1-12 shows the page for the chapter1-mvc-bookstore project (after it has been successfully deployed) and the Apache Tomcat log console.
../images/300017_2_En_1_Chapter/300017_2_En_1_Fig12_HTML.jpg
Figure 1-12

IntelliJ IDEA Apache Tomcat log console

Running the Spring Boot projects is even easier. Find the main class, right-click it, and choose Run. If the project was built successfully, the application should start and should appear in the Services view, as depicted in Figure 1-13.
../images/300017_2_En_1_Chapter/300017_2_En_1_Fig13_HTML.jpg
Figure 1-13

IntelliJ IDEA Spring Boot main application class and Services view

It seems IntelliJ IDEA has some difficulties with Gradle multi-module project because, for Spring Boot Web applications, it cannot detect the working directory, which means it cannot build an application context correctly. To fix this, open the project launcher generated for the Spring Boot application and select the directory of the project you want to run as a value for the Working directory option, as depicted in Figure 1-14.
../images/300017_2_En_1_Chapter/300017_2_En_1_Fig14_HTML.jpg
Figure 1-14

IntelliJ IDEA Spring Boot Launcher with working directory populated explicitly

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!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.187.103