Spring Boot CLI
In my previous articles titled “Introduction to Spring Boot” and “Spring Boot Components and Internals”, we have covered the fundamentals of Spring Boot and explored the usage of its four main components. Now, we will delve into one specific component of Spring Boot, namely the CLI, and examine it extensively.
What does Spring Boot CLI refer to?
Spring Boot CLI is a tool developed by Spring Boot for running and testing Spring Boot applications directly from the command line. When we use the CLI to run Spring Boot applications, it internally utilizes the Spring Boot Starter and Spring Boot AutoConfigurate components to handle all dependencies and execute the application. It also includes Groovy and Grape, which is a JAR Dependency Manager, to automatically add Spring Boot Defaults and resolve all dependencies. In this discussion, we will cover the installation, setup, and usage of the CLI in a Windows environment, although the process is similar in other environments as well.
Installing Spring Boot CLI
There are two options for installing the Spring Boot CLI software on Windows: using the Windows Installer or the Zip file. Both methods are simple and provide the same software. We will be using the Zip file method, as it is easier. Specifically, we will be installing the latest version of Spring Boot (1.2.3.RELEASE) from the following link: https://start.spring.io/ (This is a Spring Initializr Web Interface, and we will cover it in more detail in future posts.) To install and set up the Spring Boot CLI software in a Windows environment, please follow these steps.
Download Spring Boot CLI zip file using Spring Initilizr
Click on “Download Spring CLI Zip” button as shown below: Once we download Spring Boot CLI Zip file into our local FileSystem, it looks like this. – Extract spring-boot-cli-1.2.3.RELEASE.zip file into our local FileSystem.
– Set Spring Boot CLI Environment Variables in Windows System as shown below.
set PATH=D:\spring-boot-cli-1.2.3.RELEASE\bin;%PATH%
- Execute the below command to verify our installation process.
We can use “spring –version” to know the Spring Boot CLI Version as shown below.
spring --version
By executing “spring –help,” we can obtain information about the Spring Boot CLI Version, as demonstrated below.
spring --help
The installation of our Spring Boot CLI is now completed successfully. Prior to diving into the Spring Boot “HelloWorld” Example, let’s understand how to execute Groovy scripts using the command prompt.
The “spring” command in Spring Boot.
The Spring Boot CLI Software offers a “spring” command that allows you to execute Spring Boot Groovy scripts through the Command Prompt. As mentioned earlier, the “spring –help” command provides multiple options to customize the usage of this command for various purposes. The crucial option we will be utilizing here is the “run” option. The syntax for the “spring” command is as follows:
spring run <SpringBoot-Groovy-Scriptname>
I have a Groovy Script file for a Spring Boot Application. We can use this command to run our Spring Boot HelloWorld Example. Let’s start working on a basic HelloWorld Spring Boot Example using the Spring Boot CLI.
Example of HelloWorld using Spring Boot.
We are going to create an example of a Spring Boot MVC RestController. This was the initial example shared by the Pivotal team on Twitter to showcase the capabilities of the Spring Boot Framework. To create a Spring Boot HelloWorld Example, please adhere to the following steps.
- Create a “HelloWorld” Folder in our Local FileSystem to place our groovy scripts.
- Develop a Groovy script file using the following content
@RestController
class HelloWorld {
@RequestMapping("/")
String hello() {
"Hello SC World."
}
}
Please assign the name HelloWorld.groovy to this file. The file must have a mandatory extension of “.groovy”. This will be followed by an explanation of the code.
- Defined a REST Controller using Spring 4 MVC @RestController annotation.
- Defined a Mapping URL “/” using Spring MVC @RequestMapping annotation.
- Defined a method to return a String to a Client or Web Browser.
By examining our HelloWorld.groovy, we can identify the following significant points.
- No imports
- No other XML configuration to define Spring MVC Components like Views,ViewResolver etc.
- No web.xml and No DispatcherServlet declaration
- No build scripts to create our Application war file
- No need to build war file to deploy this application
So, who will be responsible for supplying all these elements to our Spring Boot HelloWorld application? Let’s run the application and observe the outcomes before addressing this inquiry. Presently, the folder structure of the Spring Boot HelloWorld Example appears as follows.
Our Spring Boot HelloWorld Example, which includes a Spring MVC RestController, is now prepared. We can proceed to execute and examine this example to understand the capabilities of the Spring Boot Framework.
Execute the Spring Boot HelloWorld Example.
To test our Spring Boot HelloWorld Example application, kindly proceed with the following instructions:
- Open command prompt at “HelloWorld” Folder in our Local FileSystem.
- Execute the following command
spring run HelloWorld.groovy
- Observe the output at “spring run” command console.
If we observe here, when we execute “spring run HelloWorld.groovy”, it starts Embedded Tomcat server at Default port number: 8080. Now our Spring Boot HelloWorld Example application is up and running. It’s time to test it now. NOTE:- If we observe the above screen shot, I have highlighted “SpringApplication” class file. Here o.s.boot.SpringApplication means org.springframework.boot.SpringApplication class. What is this SpringApplication? What is the use of SpringApplication? Please refer my upcoming posts to answer these questions.- Open browser and access the following link.
Access this URL: https://localhost:8080/ Now we are able to access our first Spring Boot MVC RESTful WebService.
When we examine this Spring Boot application, we may wonder who will be responsible for offering all these components to our Spring Boot HelloWorld application.
- No imports
- No other XML configuration to define Spring MVC Components like Views,ViewResolver etc.
- No web.xml and No DispatcherServlet declaration
- No build scripts to create our Application war file
- No need to build war file to deploy this application
The responsibility for these matters lies with the Spring Boot Core Components, the Groovy Compiler (groovyc), and the Groovy Grape (Groovy’s JAR Dependency Manager). The Spring Boot Components utilize the Groovy Compiler and Groovy Grape to provide default functionalities such as adding necessary imports, supplying required configurations, resolving jar dependencies, and incorporating the main() method, among others. As Spring Boot Developers, we don’t need to be concerned about all these things as the Spring Boot Framework will handle them for us. This is one of the advantages of the Spring Boot Framework, as it eliminates the need for excessive boilerplate code and Spring Configuration, reduces development time, and enhances productivity. In this discussion, we haven’t gone into detail about Spring Boot Annotations, Spring Boot API, the purpose of the main() method in a Spring Boot Application, etc. However, we will address these questions in future posts using Spring Boot IDEs. That concludes the explanation of Spring Boot CLI. Should you have any inquiries, please feel free to leave a comment.
see other Spring tutorials
Spring MVC HandlerInterceptorAdapter and HandlerInterceptor.(Opens in a new browser tab)
One example of Spring REST XML and JSON(Opens in a new browser tab)