Spring Boot CLI Setup and HelloWorld Example

In my previous posts “Introduction to Spring Boot”  we have discussed Spring Boot basics and three approaches to creating the Spring Boot applications. Now we will discuss the first one CLI.

 What is Spring Boot CLI?

The Spring Boot CLI is a command line tool that can be used if you want to quickly prototype with Spring. It allows you to run Groovy scripts, which means that you have a familiar Java-like syntax, without so much boilerplate code.

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.

When we run Spring Boot applications using CLI, it internally uses Spring Boot Starter and Spring Boot AutoConfigurate components to resolve the 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 CLI Installation, CLI Setup and CLI commands in Windows Environment. It’s almost similar to other Environment as well.

Spring Boot CLI Installation

http://repo.spring.io/milestone/org/springframework/boot/spring-boot-cli/

The Latest version at the time of this post is 2.0.0.M1

Download the Spring Boot CLI here.

  • Choose spring-boot-cli-2.0.0.M1-bin.zip for the Windows Environment.
  • Extract spring-boot-cli-2.0.0.M1-bin.zip file into local File System.
  • Set the Spring Boot CLI Environment in Windows as shown below.
Execute the below commands to verify the installation process.

We can use “spring –version” to know the Spring Boot CLI Version as shown below.

Now our Spring Boot CLI Installed successfully.
Before discussing Spring Boot “HelloWorld” Example, first we will see how to run Groovy scripts from the command prompt.

Spring Boot “spring” command

Spring Boot CLI Software provides a “spring” command to run Spring Boot Groovy scripts from Command Prompt. One important option, we are going to use here is “run” option.

“spring” command syntax:

Ok. Now, It’s time to work with Simple HelloWorld Spring Boot Example with Spring Boot CLI.

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

So, Whats the beauty of the above example?

If we observe HelloWorld.groovy, we can find the following important points.

  • No import statements
  • No XML configuration to define Spring MVC Components like Views, ViewResolver etc.
  • No web.xml and No DispatcherServlet
  • No build scripts
  • No need to build war file to deploy this application

Ok, let’s run the application.

Run Spring Boot HelloWorld Example

  • Open command prompt at “HelloWorld” Folder in our Local FileSystem.
  • Execute the following command
  • 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.

  • Open browser and access the following link.

Access this URL: http://localhost:8080/

That’s simple to start the application with Spring Boot CLI.

By this way, Spring Boot framework avoids lots of boilerplate code and Spring Configuration, reduces development time and increases productivity.

We will discuss the Spring Boot Annotations and much more in coming posts.

Until then Happy Coding.

harinathk
 

Click Here to Leave a Comment Below 0 comments