Statistics
8
Views
0
Downloads
0
Donations
Uploader

高宏飞

Shared on 2026-06-29

AuthorDinesh Rajput

Let us full stack development with Spring Boot and React JS. Key Features This book has a very specific goal to make developing REST applications easier and focusing on common challenges of the design of the application with best practices. This book is providing practical code examples from real-world experiences. This book is not only about Spring Boot 2.2 and React JS overview but also has an in-depth discussion about adopted REST Architectural pattern and its constraints to create the REST APIs. The book can act as a tool for learning Spring Boot 2.2 and React JS for the first time as well as a guide and reference for those wanting to dig deeper into specific features. This book is also providing deeper information about the Spring Security and JWT token-based authentication for your REST applications. This book is also providing deeper information about the Spring Security and JWT token-based authentication for your REST applications. Containerization using Docker is another key feature of this book, how to create a Docker image and how to run it. Description Designing Applications with Spring Boot 2 & React JS is divided into three parts. The first part introduces you to the essentials of the Spring Boot 2.2 Framework and you will learn how to create REST APIs and how to secure REST APIs. Part 2 steps behind the front end application development with React JS and discuss React features and its advantages toward the frontend application development. Part 3 expands on that by showing how to deploy backend and frontend application the PaaS platform and also will discuss how to deploy application container technologies such as Docker. What Will You Learn Exploring Spring Boot 2.2 new features and essential key components such as Starters, Autoconfiguration, CLI, Actuator. Develop a REST application using Spring Boot 2.2 and DevTools. Exploring Spring Boot Auto Configuration and Customization. Creating application profiles based on the environments. Learn to confi

AI Reading Assistant

Summary and highlights from this book's index; jump to passages in the text

Passage locations
Tags
No tags
ISBN: 9388511646
Publisher: BPB Publications
Publish Year: 2019
Language: 英文
Pages: 368
File Format: PDF
File Size: 8.6 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Designing Applications with Spring Boot 2.2 and React JS Step-by-step guide to design and develop intuitive full stack web applications by Dinesh Rajput FIRST EDITION 2019 Copyright © BPB Publications, India ISBN: 978-93-88511-643 All Rights Reserved. No part of this publication may be reproduced or distributed in any form or by any means or stored in a database or retrieval system, without the prior written permission of the publisher with the exception to the program listings which may be entered, stored and executed in a computer system, but they can not be reproduced by the means of publication. LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY The information contained in this book is true to correct and the best of author’s & publisher’s knowledge. The author has made every effort to ensure the accuracy of these publications, but cannot be held responsible for any loss or damage arising from any information in this book. All trademarks referred to in the book are acknowledged as properties of their respective owners. Distributors: BPB PUBLICATIONS 20, Ansari Road, Darya Ganj New Delhi-110002 Ph: 23254990/23254991 MICRO MEDIA Shop No. 5, Mahendra Chambers, 150 DN Rd. Next to Capital Cinema, V.T. (C.S.T.) Station, MUMBAI-400 001 Ph: 22078296/22078297 DECCAN AGENCIES 4-3-329, Bank Street, Hyderabad-500195
Ph: 24756967/24756400 BPB BOOK CENTRE 376 Old Lajpat Rai Market, Delhi-110006 Ph: 23861747 Published by Manish Jain for BPB Publications, 20 Ansari Road, Darya Ganj, New Delhi-110002 and Printed by him at Repro India Ltd, Mumbai Dedication To my country, India. My grandpas, the late Mr. Arjun Singh and the late Mr. Durjan Lal Rajput. To all readers of DineshOnJava and my books. To my mother, Indira Devi, and my father, Shrikrashan, for their sacrifices and for exemplifying the power of determination. To my kids, Arnav and Rushika, and my wife, Anamika, for being my loving partner throughout my life journey. About the Author Dinesh Rajput is the founder of https://www.dineshonjava.com , a blog for Spring and Java techies. He is a Spring enthusiast and a Pivotal Certified Spring Professional. He has written several bestselling IT books Like Spring 5 Design Patterns and Mastering Spring Boot 2.0. Mastering Spring Boot 2.0 is the Amazon #1 best-selling book on Java . He has more than 10 years of experience on various aspects of Spring and cloud-native development, such as REST APIs and Microservice Architecture. He is currently working as an architect at a leading company. He has worked as a tech lead at Bennett, Coleman & Co. Ltd, and Paytm. He holds a Master’s degree in Computer Engineering from JSS Academy of Technical Education, Noida, and lives in Noida with his family. His Website: https://www.dineshonjava.com His Blog: https://www.dineshonjava.com & https://www.dineshrajput.com His Linked in Profile: https://www.linkedin.com/in/rajputdinesh/ Acknowledgements I would like to start my acknowledgement with the following quote: “There is nothing unachievable in the world if you have one thing ‘let us try’ .” I would like to start by thanking Mr. Manish Jain for giving me an opportunity to write this book. I would like to express my gratitude to the BPB Publications team. It started with Vinay and Anugraha, my acquisition editors, who patiently worked with me. Along the way, my development editor challenged me to become a better author. I would like to thank Subha Nadar, my technical editor, who gavea technical shape to the book and constantly checked my work and ensured the overall quality of the examples and the code I produced. I would like to thank my family for their love and support. To my wife Anamika, you have been my best friend and the love of my life. I would also like to thank my kids, Arnav and Rushika. You both make me a better father as well as a better author and more importantly, a better person.
Finally, I would like to thank the Dinesh on java readers who bought this book early and shared their valuable feedback. I hope that you enjoy this book as much as I enjoyed writing it. Thank you! Preface Designing Applications with Spring Boot 2.2 and React JS is for all Java developers who want to learn Spring Boot and React JS in the enterprise full stack applications. Therefore, enterprise Java and Spring developers will find this book useful in understanding REST architectural design patterns using Spring Boot 2.2,how React JS helps in the development of front-end applications, how to create reusable UI components, and they will appreciate the examples presented in this book. Using Spring Boot, you can solve common design problems of the REST ful applications. Before reading this book, readers should have basic knowledge of Core Java, Spring Core Framework, REST basics, and JavaScript. Spring Boot 2.2 has been newly launched by Pivotal with the reactive programming and cloud. Spring Boot 2.2 introduces many new features and enhancements from its previous version. We will discuss Spring Boot and its essential key components in this book. Designing Applications with Spring Boot 2.2 and React JS is a full stack development book that will give you in-depth insight into Spring Boot for backend application development and React JS for front-end application development. The great part of today’s Spring Boot is that many companies have already adopted it as a primary framework for the development of backend applications, especially for the REST APIs using the microservices architecture. For Spring Boot, no external enterprise servers are needed to start working with them. React JS is also a very popular JS framework used for the development of front-end applications. The goal of writing this book is to discuss a full stack application using Spring Boot and React JS and the common designs used behind the REST ful applications. Here, the author has also outlined some best practices to understand the React JS components life cycle. The book contains 10 chapters, which cover everything from the development of a full stack application to the deployment of the front-end and backend applications by either using virtual machines or containers such as Docker. Designing Application with Spring Boot 2.2 and React JSis divided into three parts. The first part introduces you to the essentials of the Spring Boot 2.2 framework and you will learn how to create and secure REST APIs. The second part explains the steps for the development of front-end applications in React JS and discusses React features and its advantages. The third part expands on that by showing how to deploy the backend and front-end applications in the PaaS platform and finally, the fourth part explains how to deploy application container technologies such as Docker. What you need for this book This book can be read without a computer or laptop at hand, in which case you need nothing more than the book itself. However, to follow the examples in the book, you need Java 8, which you can download from http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads- 2133151.html , and you will also need your favorite IDE.I have used the Software Spring Tool Suite; download the latest version of Spring Tool Suite (STS) from https://spring.io/tools/sts/ all according to your OS. Java 8 and STS work on a variety of platforms: Windows, MacOS and Linux. Downloading the code bundle and colored images: Please follow the link to download the Code Bundle and the Colored Images of the book: https://rebrand.ly/b3bf0 Errata We take immense pride in our work at BPB Publications and follow best practices to ensure the accuracy of our content to provide with an indulging reading experience to our subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve upon human errors if any, occurred during the publishing processes involved. To let us maintain the quality and help us reach out to any readers who might be having difficulties due to any unforeseen errors, please write to us at : errata@bpbonline.com Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’ Family. Table of Contents 1. Getting Started with Spring Boot 2.2 Introduction to Spring Boot 2.2 Essential key components of Spring Boot Spring Boot Starters
Spring Boot Starters The Spring Boot auto-configuration The Spring Boot CLI Spring Boot Actuator System requirements and setting up a workspace Setting up the Spring Boot workspace Using the Maven installation for Spring Boot Using the Gradle installation Creating a Spring Boot application Initializing a Spring project with a web interface Initializing a Spring project with the STS IDE The Spring Boot application launcher file Testing the application file The build specification file Implementing a REST controller Writing a test for the controller Introducing Spring Boot DevTools Conclusion Questions 1. Customizing Auto-configuration Understanding auto-configuration Enabling Spring Boot auto-configuration Disabling the Spring Boot auto-configuration How the Spring Boot auto-configuration works? Auto-configuration classes Order of evaluation for overridden properties Customizing the name of the application properties file Application configuration using a properties file Application configuration using a YAML file Configuring an embedded server Configuring a data source Configuring Logging Multi-profile YAML documents Creating your own configuration properties Defining your own configuration properties in the application
Declaring the configuration property and metadata Profiling Activating profiles Profile-specific application configuration properties files Conclusion Questions 1. Configuring Data and CRUD operations Using JDBC with the Spring application Adding a domain class Working with JdbcTemplate Creating table schema and loading data Configuring a DataSource In-memory embedded database support Production database support A JNDI DataSource support Working with Spring Data JPA A quick introduction to ORM with JPA Creating the entity class Creating and dropping JPA databases Introduction to Spring Data Configuring Spring Data JPA to the project Creating Spring Data JPA repositories 80 Creating ProductRepository using the Repository marker interface Creating ProductRepository using the CrudRepository interface Using CommandLineRunner Using the data.sql file Customizing Spring Data JPA repositories Using the @Query annotation Using pagination and sorting Configuring the H2 Database Configuring the MariaDB database Conclusion Question 1. Creating REST APIs with Spring Boot 2.2
An introduction to the REST architectural style REST architectural constraints Client-server Stateless Cacheable Layered system Code-on-demand A uniform interface The uniform interface principle Identifying the resources Resource representation Self-descriptive messages Hypermedia as the Engine of Application State (HATEOAS) 100 Create a RESTful web service with Spring Boot 102 Using the @RestController annotation Retrieving data from the server Sending data to the server Updating data on the server Deleting data from the server Adding Hypermedia to the RESTful APIs Using Spring HATEOAS Using the Resource and Resources classes Implementing the resource assemblers Changing the embedded relationship name Create a RESTful web service using Spring Data REST Consuming REST endpoints Consuming REST endpoints with RestTemplate Retrieving resources using the GET method Creating resource data using the POST method Updating resources using the PUT method Deleting resources using the DELETE method Consuming REST endpoints with Traverson Consuming REST endpoints with WebClient Conclusion 1. Securing REST APIs
Spring Security Adding the Spring Security module Implementing and configuring Spring Security An in-memory user configuration A JDBC-based user configuration An LDAP-backed user configuration A custom user details configuration Password encoding with Spring Security Securing your REST APIs using Spring Security and JWT Securing your REST APIs with Spring Security and OAuth2 Key components for the OAuth2 architecture An authorization server Resource server OAuth2 OAuth2 tokens The OAuth2 authorization flow diagram Implementing the OAuth2 Server with Spring Boot Security Authorization server configuration Implementing the resource server with Spring Boot Security Resource server configuration Implementing the client-server application Conclusion Questions 1. Testing a Spring Boot Application Testing in Spring Boot Creating unit tests Creating integration tests Testing the controllers Testing auto-configured data JPA repository Loading test configurations Activating profiles for a test class Conclusion Questions 1. Getting Started with React JS
Introducing React Features of React JS Declarative behaviour Virtual DOM object Event handling model JSX Component-based approach React native Advantages of React JS Apps performance Code reusability Code readability Across platforms Limitations of React JS Setting up the environment for React JS Installing Node.js and NPM Installing the Visual Studio Code Editor Creating a React application Using webpack and babel Using the create-react-app command Conclusion Questions 1. Creating React JS Components React JSX Using the container for nested elements HTML tags in lowercase Using the custom attribute in an HTML tag Using Style in the JSX code Adding JavaScript expressions in JSX React components Data flow in the React components Event flow in the React components Creating React components for the PRODOS front-end application Using multiple React components Using properties (props) in React components
Using state in React components Creating Header and Footer components to the Prodos front-end application Handling lists with React and creating a table React component for our Prodos React application Handling events with React Handling forms with React Using input text fields in form with React component Adding multiple input text fields in the form The React component lifecycle Conclusion Questions 1. Consuming REST API with React Using REST services in a React application Using the REST services with the Fetch API Fetching data using the Fetch API Posting data using the Fetch API Editing data using the Fetch API Deleting data using the Fetch API Using third-party React components in our application Using the ReactTable Using React Skylight Using the Toast message React component Using the react-confirm-alert Component Conclusion Questions 1. Deploying and Containerizing Applications Deploying applications to the Cloud platform Deploying the Spring Boot backend application Deploying the React JS front-end application Introducing containers Understanding how a container works An implementation of the container Benefits of a container-oriented approach Consistent Faster processing
Portable Light weight Efficient Dependencies Getting started with Docker Installing Docker Installing Docker on Linux Installing Docker on Windows Deploy using the Docker container Writing Dockerfile Creating a Docker image using the Maven plugin Creating a Docker image using the Docker command Conclusion Questions Index Chapter 1 Getting Started with Spring Boot 2.2 Spring Boot is a tricky framework to understand. In this chapter, we will discuss Spring Boot 2.2 and the underlying important concepts like starter projects, auto-configuration, and starter parents. We will also discuss how Spring Boot makes software development easy. As a bonus, I will discuss the story behind the success of Spring Boot. This chapter will cover a demo application with Spring Boot and create a REST service. In this chapter, we will cover the following topics: Introduction to Spring Boot 2.2 Essential key components o Starters o Auto-configuration o The Spring Boot CLI o The Spring Boot Actuator System requirements and setting up the Spring Boot workspace Creating a Spring Boot application Introduction to Spring Boot DevTools Introduction to Spring Boot 2.2 Spring Boot is one of the most popular frameworks used to develop software for any enterprise application. It is useful to develop a modern cloud- based distributed system such as microservices-based application. Spring Boot was one of the very major projects of the Pivotal team and it was launched in 2013. Spring Boot makes it easy to create standalone, production-grade Spring-based applications that you can run. We take an opinionated view of the Spring platform and third-party libraries so that you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
Spring Boot is built on top of the existing Spring framework. So basically, it is not a separate framework, but it is similar to the existing Spring framework with some features. It’s a collection of ready-made things to just pick and use without taking any overhead configuration. The primary goals of the Spring Boot are as follows: Spring Boot provides faster Spring application development Eliminate a lot of configurations from the application development and provide a new strategy for application development with minimal fuss. You just focus on the application functionality rather than investing a lot of time in meta-configuration. Spring Boot is a very opinionated framework out-of-the-box. It never forces to use auto-configuration for any module. We can easily override 100% configuration for any module if requirements start to diverge from the defaults. Spring Boot does not generate code for you. It provides auto-configuration based on the module’s libraries available on the classpath of the application. Apart from the auto-configurations, Spring Boot also provides a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics, health checks, and externalized configuration). Spring Boot reduces friction of project dependency management in the spring application. As we know that the dependency management and deciding versions of depending libraries are very hectic work for any software engineer. Spring Boot allows you to create a Java application such as JAR and WAR artifact. The Spring team is continuously working on the Spring framework and provides very interesting features till now. Spring Boot is one of the main projects of the Spring team. Spring Boot is not a separate framework or code generation library. It is a Spring framework by heart that means Spring Boot is on top of the Spring framework with some auto-configuration for modules based on the libraries available on the classpath of the Spring application. Let’s see the following diagram: Figure 1.1: The Spring Boot framework representation In the preceding diagram, you can see the Spring Boot framework on the top of the Spring framework with all the modules such as Spring MVC, Security, JDBC, ORM, Batch, JPA, and so on. Internally, Spring Boot uses the Spring framework to provide core functionality of Spring such as bean creation, providing bean’s scopes, etc. Let’s suppose you want to develop an application with Spring MVC and Spring Security, so you just need to add the libraries of these modules to the classpath of the application. You do not need to add configurations of Spring MVC and Spring Security to your application. Spring Boot adds this configuration by default in your application based on the libraries available in the classpath of the application. You can still override the default configuration of these modules. Spring Boot never forces you to use the default configuration. Further, let’s discuss the key components of Spring Boot in the next section. Essential key components of Spring Boot
Spring Boot simplifies the Spring application development, so some magical components are behind this. The four essential key components of Spring Boot are as follows: Spring Boot Starters Auto-configuration Spring Boot CLI Spring Boot Actuator Let’s discuss these key components in detail. Spring Boot Starters Starters in Spring Boot are like small Spring projects for each module with all the required configurations such as Spring Web MVC, Spring JDBC, ORM, JPA, Security, Spring Batch, and so on. According to the Spring Boot documentation, Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all Spring and related technologies that you need, without having to hunt through sample code and copy-paste loads of dependency descriptors . In your Spring Boot application, add the starters of the respective modules to the classpath of your application and Spring Boot will ensure that all dependencies and transitive dependencies of the modules is using Maven or Gradle. Let’s suppose you want to develop a REST web application to expose RESTful web services. For this REST application, you need to add the Spring web MVC module to your REST web application by including the springboot-starter-web dependency in your project using Maven build, as shown in the following code: org.springframework.boot spring-boot-starter-web You can also add the same springboot-starter-web dependency to your project using a Gradle build as shown in the following code: dependencies { implementation ‘org.springframework.boot:spring-boot-starter-web’ ... } The preceding Maven and Gradle dependencies configuration for the Spring web MVC module resolves the following transitive dependencies: spring-web-*.jar spring-webmvc-*.jar tomcat-*.jar jackson-databind-*.jar Similarly, you can add the other modules to the same application such as ORM, Security, Spring Test, Spring Data JPA, and so on by including respective starters of these modules as follows: spring-boot-starter-test: This is used to write unit and integration tests. spring-boot-starter-security: This is used for authentication and authorization using Spring Security. spring-boot-starter-data-jpa: This uses Spring Data JPA with Hibernate. spring-boot-starter-cache: This is used to enable the Spring framework’s caching support. spring-boot-starter-data-rest: This is used to expose simple REST services using Spring Data REST. Spring Boot provides a lot of starters in the Spring Boot project under the org. springframework.boot group.
The version of the starters in the Spring Boot application is managed by the Spring Boot starter parent POM in the Maven build application. The parent starters in the POM file are as follows: org.springframework.boot spring-boot-starter-parent 2.2.0.BUILD-SNAPSHOT The preceding Spring Boot Starter parent POM manages the versions of the all child modules in the project. And also, it manages the default Maven build plugins in the Spring Boot application. Let’s discuss another magical key component of the Spring Boot framework. The Spring Boot auto-configuration The Spring Boot auto-configuration is one of the most essential key features of Spring Boot. Spring Boot detects the libraries in the classpath when the application starts. Spring Boot automatically configures the beans in the Spring application context to enable the Spring’s module. In the previous section, we added the Spring web MVC starter to our Spring application. In this case, Spring Boot will automatically configure the beans in the Spring application context to enable Spring MVC. It will also configure the embedded Tomcat server in the Spring application context. This means the Spring Boot auto-configuration does all work related to the configuration, for you to just write your business functionality code. If you forget a dependency for a specific module, Spring Boot cannot configure this module. We will discuss more about the Spring Boot auto-configuration in the next Chapter 2: Customizing Auto-Configuration . Further, let’s see another component of Spring Boot in the next section. The Spring Boot CLI The Spring Boot framework has a command-line interface used to create a Spring application quickly using some commands provided by Spring Boot. You can use Groovy scripts to create very fast Spring applications and the Spring Boot CLI allows you to run the Groovy scripts on the CLI. Spring Boot support Groovy language to create a Spring application with almost zero boilerplate code compared to Java. The Spring Boot documentation says: You don’t need to use the CLI to work with Spring Boot but it’s definitely the quickest way to get a Spring application off the ground. The Spring Boot CLI can be downloaded from the official site of the Spring framework: https://repo.spring.io/snapshot/org/springframework/boot/spring-boot-cli/2.2.0.BUILD-SNAPSHOT/spring-boot-cli-2.2.0.BUILD-SNAPSHOT-bin.zip https://repo.spring.io/snapshot/org/springframework/boot/spring-boot-cli/2.2.0.BUILD-SNAPSHOT/spring-boot-cli-2.2.0.BUILD-SNAPSHOT- bin.tar.gz Starters, dependencies and auto-configuration can be easily added to the Spring application using the Spring Boot CLI. The Spring Boot CLI allows you to focus only on writing your application-specific code. Let’s see the following Groovy script: @RestController class HelloController { @GetMapping(“/”) String hello() { return “Hello World!!!” } } Let’s save the above Groovy file as app.groovy in a directory and you can run this file on the Spring Boot CLI using the following command:
$ spring run app.groovy The preceding Groovy script is not only a simple controller class but it is also a whole application, and you can also run this application and access it from http://localhost:8080/ . You can see the output Hello World in the browser. Figure 1.2: Groovy application output in the browser This means the Spring Boot CLI is a smart tool because you do not need to add any dependencies to build files such as Maven’s pom.xml or Gradle’s build.gradle. There is no import required in the Groovy script. The Spring Boot CLI detects the following: Dependencies and libraries using classes being used in the application. Auto-configuration based on the classes in the application. Let’s move on to another key component of Spring Boot’s building blocks. This is a Spring Boot Actuator that gives us insight about running a Spring Boot application. Spring Boot Actuator We discussed the three key components of Spring Boot to simplify the Spring development. But a final key component of Spring Boot is Actuator. It provides post-production grade features. You can monitor application health, metrics, and other monitoring features using Spring Boot Actuator in the production. It provides HTTP endpoints to monitor your Spring application during the production. The Actuator provides some of the following benefits: The Actuator provides various metrics such as memory usage, web requests, garbage collection, and data source usage. It gives all details about the configured beans in the Spring application context. You can get all details about the Spring Boot auto-configuration using the Actuator. Also, you can get other configurations such as environment variables, system properties, configuration properties, and command-line arguments. It provides a trace of recent HTTP requests handled by your application. It also gives information about the current state of the threads in the Spring Boot application. We discussed all the four essential key components of Spring Boot. These components work to simplify the Spring development. Let’s move on to the next section and discuss system requirements for the Spring Boot 2.2.0 based Spring application and set up the Spring Boot workspace. System requirements and setting up a workspace Spring Boot 2.2.0 recently released the framework with advanced features which we will discuss later in this chapter. The minimum system requirement for Spring Boot 2.2.0 is as follows: Spring Boot 2.2.0 requires Java 8 and is compatible up to Java 11. This version 2.2.0 of Spring Boot has the Spring framework version 5.1.4.RELEASE. It provides build support for the following build tools: Spring Boot 2.2.0 provides support for the following embedded servlet containers: The minimum requirement of the servlet container for Spring Boot 2.2.0 application is Servlet 3.1+. Let’s set up the Spring Boot in your machine. Setting up the Spring Boot workspace
Before we set up Spring Boot in your machine, please ensure your Java version in your machine with the following command: $ java -version You can use Spring Boot as a classical Java development tool. There is no specific tool or libraries integration required for Spring Boot. You need to include the spring-boot-*.jar files in the classpath of your application. You can use any IDE for the Spring Boot development (But I personally like to use Spring Tools Suits). Let’s see the following ways to set up the workspace for the Spring Boot application: Using the Maven installation for Spring Boot Using the Gradle installation for Spring Boot Now, we will explore how to set up a Spring Boot application with Maven and Gradle in detail. Using the Maven installation for Spring Boot Spring Boot 2.2.0 is compatible with Maven 3.3+. If your machine does not have it, let’s install it. You will find the installation instructions at https://maven.apache.org/ . Also, ensure your Java 8+ and you will find it at https://www.oracle.com/technetwork/java/javase/downloads/index.html . Spring Boot uses the Maven dependencies with groupId as org.springframework.boot and your Maven configuration file POM inherits the Spring dependency version management and Maven plugin to create executable JARs from the parent project with artifcatId as spring-boot-starter-parent. Let’s see the following Maven dependency pom.xml file: xsi:schemaLocation=” http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd ”> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.0.RELEASE com.dineshonjava myproject 0.0.1-SNAPSHOT myproject Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web
org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin The preceding pom.xml file has the bare minimum requirement for the Spring Boot 2.2 application with the Spring MVC web module. Let’s discuss another way of Spring Boot installation that is Gradle setup for the Spring Boot application. Using the Gradle installation Spring Boot 2.2.0 is compatible with Gradle 4.4+. If your machine does not have it, let’s install it. You will find the installation instructions at https://gradle.org/ . But it requires Java 8+ for both installations either with Maven or Gradle. Now let’s see the following dependencies for the Spring Boot with groupId as org.springframework.boot: buildscript { ext { springBootVersion = ‘2.2.0.RELEASE’ } repositories { mavenCentral() } dependencies { classpath(“org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}”) } } apply plugin: ‘java’ apply plugin: ‘org.springframework.boot’ apply plugin: ‘io.spring.dependency-management’
group = ‘com.dineshonjava version = ‘0.0.1-SNAPSHOT’ sourceCompatibility = ‘1.8’ repositories { mavenCentral() } dependencies { implementation ‘org.springframework.boot:spring-boot-starter-web’ testImplementation ‘org.springframework.boot:spring-boot-starter-test’ } The preceding build.gradle file has the bare minimum requirement for the Spring Boot 2.2 application with the Spring MVC web module. After setting up the Spring Boot workspace either with Maven or Gradle, let’s create a Spring Boot application and see how to set up the project structure using Spring Boot Initializr in the next section. Creating a Spring Boot application In this section, we will create a simple Hello World REST web application using some of Spring Boot’s key features. In this REST application, we will create a simple REST service that will return the Hello World message on request. We will use Maven to build this project. Let’s create a project for this simple example. Spring Boot allows us to create the Spring Boot application project structure using the Spring Boot Initializr. The Spring Boot Initializr provides solutions to all problems related to set up work, and it creates a more traditional Java project structure. You can use the Spring Initializr to create a skeleton Spring project structure. It is both a browser-based web application and a REST API. You can use it in the following ways: The Spring Initializr can be used from the web interface at https://start.spring.io/ . The Spring Initializr can be used from the command line using the curl command. The Spring Initializr can be used from the command line using the Spring Boot command-line interface. You can use the Spring Initializr by creating a new project with Spring Tool Suite. You can use the Spring Initializr by creating a new project with IntelliJ IDEA. You can use the Spring Initializr by creating a new project with NetBeans. We have seen several ways to use the Spring Initializr to create a Spring Boot application skeleton, but in this chapter, we are not going to discuss all of them. My favourite IDE is Spring Tool Suite ( STS ); I will use it in this book throughout chapters. Let’s start from the Spring Initializr using the web interface at https://start.spring.io/ . Initializing a Spring project with a web interface A web application hosted at https://start.spring.io/ by the Spring team can be used to create a Spring Boot application. It is one of the simplest ways to use the Spring Initializr. You can choose the project build tool either from Maven or Gradle. Also, it has the option to choose languages either from Java, Kotlin, and Groovy. Finally, we have the version of Spring Boot. Let’s see the following screenshot of how the home page looks like:
Figure 1.3: The Spring Initializr web interface In the preceding screenshot, there are a lot of options to choose from to create a Spring Boot application, which are as follows: Project build tools, Maven and Gradle Programming languages (currently supported languages are Java, Kotlin, and Groovy) Spring Boot version On the left-hand side, there is a Project Metadata section. You can provide project metadata such as group ID, artifact ID, packaging, java version, and so on. On the right-hand side, there is an application Dependencies section; you can select dependencies from here. Let’s select dependencies for Spring Web MVC. Just type web and select, and then click on the Generate Project button. It will create a Spring Boot application project skeleton. Initializing a Spring project with the STS IDE The Spring Tool Suite is one of my favourite IDEs for Java development. It is also popular among many Java developers and used to create Spring-based application. You can install it from http://spring.io/tools/ . Let’s see the following screenshot to display the menu structure: Figure 1.4: Starting a new project in STS with the Spring Starter project After selecting Spring Starter Project , a new window appears, as shown in the following screenshot:
Figure 1.5: Specify project information As you can see in the preceding screenshot, the first page asks you to fill the project information such as project name, project build tool, packaging, language, group, artifact, and other essential information and then click on Next . In the next page of the wizard, you can select Maven dependencies to be added to your Spring project, as shown in the following screenshot: