In my previous posts “Introduction to Spring Boot” and “Spring Boot Components and Internals”, we have discussed about Spring Boot basics and four major Spring Boot components usage. Now we will pickup one Spring Boot Component: CLI and discuss it in details.
Spring Boot CLI(Command Line Interface) is a Spring Boot software to run and test Spring Boot applications from command prompt. When we run Spring Boot applications using CLI, then it internally uses Spring Boot Starter and Spring Boot AutoConfigurate components to resolve all dependencies and execute the application. It internally contains Groovy and Grape (JAR Dependency Manager) to add Spring Boot Defaults and resolve all dependencies automatically. We are going to discuss about CLI Installation, CLI Setup and CLI commands in Windows Environment. It’s almost similar to other Environment too.
We can install Spring Boot CLI software using either Windows Installer or Zip file. Both approaches are easy to install and will give us same Spring Boot CLI software. We are going to use easy method that is using Zip file. We are going to use Spring Boot Latest version: 1.2.3.RELEASE We can download Spring Boot CLI software at: https://start.spring.io/ (It is a Spring Initilizr WebInterface. We will discuss about this component in detail in coming posts.) Please follow these steps to Install and setup Spring Boot CLI software in Windows Environment.
set PATH=D:\spring-boot-cli-1.2.3.RELEASE\bin;%PATH%
spring --version
We can use “spring --help” to know the Spring Boot CLI Version as shown below.
spring --help
Now our Spring Boot CLI Installation process is done successfully. Before discussing about Spring Boot “HelloWorld” Example, first we will see how to run Groovy scripts from command prompt.
Spring Boot CLI Software provides a “spring” command to run Spring Boot Groovy scripts from Command Prompt. As we saw just before, Spring Boot “spring --help” command have many options to use this command for different purposes. One important option, we are going to use here is “run” option. “spring” command syntax:
spring run <SpringBoot-Groovy-Scriptname>
Here is a Groovy Script filename of a Spring Boot Application. We will use this command to execute our Spring Boot HelloWorld Example. It’s time to work with Simple HelloWorld Spring Boot Example with Spring Boot CLI.
Now we are going to develop a Spring Boot MVC RestController Example. It was the first example published on Twitter from Pivotal team to demonstrate Spring Boot Framework power. Please follow the following steps to develop a Spring Boot HelloWorld Example:
@RestController
class HelloWorld {
@RequestMapping("/")
String hello() {
"Hello JournalDev World."
}
}
Name this file as HelloWorld.groovy. Here “.groovy” extension is mandatory. Code Explanation
Code Observations If we observe our HelloWorld.groovy, we can find the following important points.
Then who will provide all these things to our Spring Boot HelloWorld application? First run the application and see the results, then we will answer this question.- Now Spring Boot HelloWorld Example folder looks like this.
Now our Spring Boot HelloWorld Example is ready with Spring MVC RestController. It’s time to run and test this example to know the Power of Spring Boot Framework.
Please follow the following steps to test our Spring Boot HelloWorld Example application:
spring run HelloWorld.groovy
https://localhost:8080/
Now we are able to access our first Spring Boot MVC RESTful WebService.If we observe this Spring Boot application, then we may get this question in our mind: who will provide all these things to our Spring Boot HelloWorld application?
Answer to this question: It is the responsibility of Spring Boot Core Components, Groovy Compiler (groovyc) and Groovy Grape (Groovy’s JAR Dependency Manager). Spring Boot Components uses Groovy Compiler and Groovy Grape to provide some Defaults lime adding required imports, providing required configuration, resolving jar dependencies, adding main() method etc. As a Spring Boot Developer, We don’t need to worry all these things. Spring Boot Framework will take care of all these things for us. That’s the beauty of Spring Boot Framework. By this way, Spring Boot framework avoids lots of boilerplate code and Spring Configuration, reduces development time and increases productivity. Here we have not discussed much about Spring Boot Annotations, Spring Boot API, What is the use of main() method in Spring Boot Application etc. We will answer all these questions in coming posts using Spring Boot IDEs. That’s it about Spring Boot CLI. If you have any questions, please drop a comment.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
How to add dependency of specific group, artifact and version?
- Ankitha
How do I stop the application?
- Cathy
Brilliant explanation! Keep going!
- Olga
How to change default port 8080 to something different?
- satra
How will the spring boot behave if we deploy in a application server, Do we need to add the configurations for the artifact identification.
- Kiran Kumar
Thanks for your tutorial.Very Helpful
- indira
Can we install CLI on mac as well? i don’t know if it makes sense when we already have a terminal .
- Santhosh
what if I want to create a Java application? can I create a Java application with CLI?
- Santhosh
It would be far easier to install the spring CLI using SDKMAN! https://sdkman.io
- Marco