Spring Boot Groovy CLI

Spring Boot Groovy CLI tutorial is an introductory tutorial to Spring Boot framework using command line interface. In this tutorial we use Groovy language.

Spring is a popular Java application framework and Spring Boot is an evolution of Spring which helps create stand-alone, production-grade Spring based applications easily. Spring supports Java and Groovy languages; Kotlin language will be supported from Spring 5.

Spring Boot CLI

Spring Boot command line interface (CLI) is a way to create Spring Boot applications using command line tools. Spring Boot CLI helps developer to create and run Spring application easily.

Note that Spring Boot CLI can be used to create both Java and Groovy applications.

Installing Spring Boot CLI

The main Spring Boot CLI command is a Bash script that is an interface to the Spring Boot loader.

$ wget http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/1.5.7.RELEASE/spring-boot-cli-1.5.7.RELEASE-bin.zip

We download the CLI tools with wget from Spring repositories.

$ unzip spring-boot-cli-1.5.7.RELEASE-bin.zip 

We unzip the ZIP archive.

$ cd spring-1.5.7.RELEASE/
$ ls
bin  INSTALL.txt  legal  lib  LICENCE.txt  shell-completion

In the bin directory, there is the spring tool that is the main command to create Spring Boot applications.

$ export PATH=$PATH:~/bin/spring-1.5.7.RELEASE/bin/

We can add the home directory of the tool to the PATH variable for convenient access to the command.

Spring Boot Groovy application

Spring Boot Groovy applications can be used for rapid application development and prototyping of Spring applications. Spring tries hard to simplify programming with Groovy. For a Groovy application, Spring does a lot of magic behind the scenes:

A Groovy application is started with the spring run command.

Simple Groovy application

The following is a simple Spring Boot Groovy application.

first.groovy
@RestController
class MyApp {

    @RequestMapping("/")
    String home() {
        "This is Spring Boot application"
    }
}

The application returns a text message back to the client.

@RestController

The @RestController indicates a Restful web application. The returned string does not indicate a name of the template file; it is directly returned to the client.

@RequestMapping("/")

With the @RequestMapping annotation, we map the method to the specified URL path.

$ spring run first.groovy
$ curl localhost:8080
This is Spring Boot application

We run the application with the spring run command and test it with curl. We did not create a Maven or Gradle build file, we did not do any configuration; everything was done automcatically by Spring behind the scenes.

Templating with Freemarker

In the following example, we create a simple Groovy web application utilizing the Freemarker template engine.

$ tree
.
├── app.groovy
├── static
│   └── index.html
└── templates
    └── hello.ftl

We have this project structure. Static files are located in the static directory and template files in the templates directory.

index.html
<!DOCTYPE html>
<html>
    <head>
        <title>Home page</title>
        <meta charset="UTF-8">
    </head>
    <body>
                
        <form action="/greet">
            <label>Enter your name:</label>
            <input type="text" name="name">
        
            <button type="submit">Submit</button>
        </form>
    </body>
</html>

This is the home page. It contains a form to send data to the web application.

app.groovy
@Controller
@Grab('spring-boot-starter-freemarker')
class MyApp {

    @RequestMapping("/greet")
    String home(Model model, @RequestParam String name) {
        
        model.addAttribute("myname", name)
        return "hello"
    }
}

This is the main Groovy application file.

@Controller

The @Controller annotation creates a classic MVC web application. The string returned from a method is a name of the template file to be processed.

@Grab('spring-boot-starter-freemarker')

Since Spring cannot deduce that we are using Freemarker, we have to tell Spring to use it with the @Grab. Spring then automatically configures Freemarker for us; the Freemarker templates are located in the templates directory.

@RequestMapping("/greet")
String home(Model model, @RequestParam String name) {
    
    model.addAttribute("myname", name)
    return "hello"
}

The home() method is mapped with the @RequestMapping annotation to the /greet path. With the @RequestParam, we retrieve the request parameter sent by the client. We add the parameter to the model and forward the processing to the hello.ftl template file.

hello.ftl
<!DOCTYPE html>
<html>
    <head>
        <title>Home page</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <p>Hello ${myname}, today is a beautiful day!</p>
    </body>
</html>

The hello.ftl template file sends an HTML page with a message. The message contains the name of the person sent from the HTML form. The value from the model is shown with the Freemarker ${} syntax.

In this tutorial, we have created simple Spring applications in Groovy with Spring Boot CLI.

You might also be interested in the related tutorials: Spring Boot @RequestParam tutorial, Spring Boot @ResponseBody tutorial, Spring Boot REST H2 tutorial, Freemarker tutorial, Java tutorial.