Tutorial

Spring Boot CLI Setup and HelloWorld Example

Published on August 4, 2022
author

Rambabu Posa

Spring Boot CLI Setup and HelloWorld Example

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.

What is Spring Boot CLI?

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.

Spring Boot CLI Installation

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.

  • Download Spring Boot CLI zip file using Spring Initilizr Click on “Download Spring CLI Zip” button as shown below: download-springboot-cli Once we download Spring Boot CLI Zip file into our local FileSystem, it looks like this. springboot-cli-software- Extract spring-boot-cli-1.2.3.RELEASE.zip file into our local FileSystem. springboot-cli-home- 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. springboot-cli-version We can use “spring --version” to know the Spring Boot CLI Version as shown below.
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 “spring” command

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.

Spring Boot HelloWorld Example

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:

  • 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 JournalDev World."
  }
}

Name this file as HelloWorld.groovy. Here “.groovy” extension is mandatory. Code Explanation

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

Code Observations If we observe our HelloWorld.groovy, we can find the following important 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

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. springboot-cli-helloworld

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.

Run Spring Boot HelloWorld Example

Please follow the following steps to test our Spring Boot HelloWorld Example application:

  • 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. springboot-cli-start 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/ springboot-helloworld-output 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?

  • 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

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.

Learn more about our products

About the authors
Default avatar
Rambabu Posa

author

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.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
December 27, 2020

How to add dependency of specific group, artifact and version?

- Ankitha

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    March 11, 2020

    How do I stop the application?

    - Cathy

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      December 18, 2018

      Brilliant explanation! Keep going!

      - Olga

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        September 20, 2018

        How to change default port 8080 to something different?

        - satra

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          July 4, 2017

          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

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            April 15, 2017

            Thanks for your tutorial.Very Helpful

            - indira

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              April 9, 2017

              Can we install CLI on mac as well? i don’t know if it makes sense when we already have a terminal .

              - Santhosh

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                December 8, 2016

                what if I want to create a Java application? can I create a Java application with CLI?

                - Santhosh

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  August 3, 2016

                  It would be far easier to install the spring CLI using SDKMAN! https://sdkman.io

                  - Marco

                    Try DigitalOcean for free

                    Click below to sign up and get $200 of credit to try our products over 60 days!

                    Sign up

                    Join the Tech Talk
                    Success! Thank you! Please check your email for further details.

                    Please complete your information!

                    Featured on Community

                    Get our biweekly newsletter

                    Sign up for Infrastructure as a Newsletter.

                    Hollie's Hub for Good

                    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                    Become a contributor

                    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                    Welcome to the developer cloud

                    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                    Learn more