Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

1. Overview

Spring Roo is a Rapid Application Development (RAD) tool that aims to deliver fast, and instant results focused on Spring web applications and newer Spring technologies. It allows us to generate boilerplate code and project structure for Spring applications with simple to use commands.

Roo can be used as a standalone application running from the operating system command line. There's no requirement to use Eclipse, Spring Tool Suite (STS) or any other IDE; in fact, we can use any text editor to write code!

However, for simplicity, we will use STS IDE with the Roo Extension.

2. Installing Spring Roo

2.1. Requirements

To follow this tutorial, these have to be installed:

  1. Java JDK 8
  2. STS
  3. Spring Roo

2.2. Installation

Once we download and install Java JDK and STS, we need to unzip Spring Roo and add it to the system path.

Let's create the ROO_HOME environment variable and add %ROO_HOME%\bin to the path.

To verify that Roo is installed correctly, we can open the command line and execute following commands:

mkdir baeldung
cd baeldung
roo quit

After a few seconds we will see:

                _
 ___ _ __  _ __(_)_ __   __ _   _ __ ___   ___
/ __| '_ \| '__| | '_ \ / _` | | '__/ _ \ / _ \
\__ \ |_) | |  | | | | | (_| | | | | (_) | (_) |
|___/ .__/|_|  |_|_| |_|\__, | |_|  \___/ \___/
    |_|                 |___/          2.0.0.RC1

Welcome to Spring Roo. For assistance press TAB or type "hint" then hit ENTER.

Roo is installed, and it is working. Please note that Spring Roo version will vary and the steps and instructions may depend on the actual version used.

Important: Spring Roo 2.0 is not backward compatible with 1.x.

2.3. Adding and Configuring STS Extension

Out-of-the-box, STS supports a development of Spring applications and includes ready-to-use extensions. However, Spring Roo extension is not included. Therefore, we will need to add it manually.

In STS let's go to Install New Software and import bookmarks to Available Software Sites. Currently, bookmarks are in the %ROO_HOME%\conf folder. Once we imported the bookmarks, we can search for simply roo and install the latest version of Spring IDE Roo Support. In the end, we will be asked to restart STS.

For detailed and up-to-date steps we can always go and check Spring Roo Getting Started documentation.

Once we have installed Roo Support in STS, we need to set up the extension. It is as easy as pointing Roo Support to %ROO_HOME% folder. Again, Spring Roo Getting Started gives detailed steps how to do it.

Now we can go to “Window” application menu once more and select Show View > Roo Shell.

3. First Project

3.1. Setting up a Project in STS

In STS let's open the Roo Shell window and click on Create New Roo Project icon. This will open a New Roo Project window.

We will name the project roo and use com.baeldung as our top level package name. We can leave all other default values and proceed to the end to create a new project using Roo.

In STS this will run the following command for us:

project setup --topLevelPackage com.baeldung --projectName "roo" --java 8 --packaging JAR

As already mentioned, we don't need an IDE, and we could have run that command from Roo Shell ourselves! For simplicity, we are using built-in features of STS.

If we get the following error:

Could not calculate build plan: Plugin org.codehaus.mojo:aspectj-maven-plugin:1.8 
  or one of its dependencies could not be resolved: 
  Failed to read artifact descriptor for org.codehaus.mojo:aspectj-maven-plugin:jar:1.8

the easiest way to fix it is to manually edit pom.xml file and update aspectj.plugin.version from 1.8 to 1.9:

<aspectj.plugin.version>1.9</aspectj.plugin.version>

At this stage, there shouldn't be any errors in the project, and there will be a few auto-generated files for us.

3.2. Roo Shell

Now it is time to familiarize with the Roo Shell. Spring Roo's primary user interface is, in fact, command prompt!

Therefore, let's go back to Roo Shell window. In it, let's run the first command by typing ‘h' and pressing CTRL+SPACE:

roo> h

help    hint

Roo will automatically suggest and autocomplete commands for us. We can type ‘hi', press CTRL+SPACE, and Roo will auto-suggest hint command.

Another great feature of Roo Shell is the context awareness. For example, the output of the hint command will change depending on the previous input.

Let's now execute the hint command and see what happens:

roo> hint 
Roo requires the installation of a persistence configuration.

Type 'jpa setup' and then hit CTRL+SPACE. We suggest you type 'H'
then CTRL+SPACE to complete "HIBERNATE".

After the --provider, press CTRL+SPACE for database choices.
For testing purposes, type (or CTRL+SPACE) HYPERSONIC_IN_MEMORY.
If you press CTRL+SPACE again, you'll see there are no more options.
As such, you're ready to press ENTER to execute the command.

Once JPA is installed, type 'hint' and ENTER for the next suggestion.

It gives us the next steps we need to perform. Let's add a database now:

roo> jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY 
Created SRC_MAIN_RESOURCES\application.properties
Updated SRC_MAIN_RESOURCES\application.properties
Updated SRC_MAIN_RESOURCES\application-dev.properties
Updated ROOT\pom.xml [added dependencies org.springframework.boot:spring-boot-starter-data-jpa:, org.springframework.boot:spring-boot-starter-jdbc:, org.hsqldb:hsqldb:; added property 'springlets.version' = '1.2.0.RC1'; added dependencies io.springlets:springlets-data-jpa:${springlets.version}, io.springlets:springlets-data-jpa:${springlets.version}; added dependencies io.springlets:springlets-data-commons:${springlets.version}, io.springlets:springlets-data-commons:${springlets.version}]

At this stage, we will need to execute some commands. Between each of them, we can always run hint command to see what is suggested by Roo. This is a very useful feature.

Let's run the commands first, and we will go through them afterward:

roo> 
entity jpa --class ~.domain.Book
field string --fieldName title --notNull 
field string --fieldName author --notNull 
field string --fieldName isbn --notNull 
repository jpa --entity ~.domain.Book
service --all 
web mvc setup
web mvc view setup --type THYMELEAF 
web mvc controller --entity ~.domain.Book --responseType THYMELEAF

We are now ready to run our application. However, let's step back through the commands to see what we have done.

First, we created a new JPA persistent entity in the src/main/java folder. Next, we created three String fields in Book class, gave them a name and set to be not null.

After that, we have generated Spring Data repository for the specified entity and created a new service interface.

In the end, we included Spring MVC configuration, installed Thymeleaf and created a new controller that manages our entity. Because we have passed Thymeleaf as the response type, the generated methods and views will reflect this.

3.3. Running the Application

Let's refresh the project and right click on roo project and select Run As > Spring Boot App.

Once the application has started, we can open a web browser and go to http://localhost:8080. Next, to Roo icon, we will see Book menu and underneath two options: Create Book and List Books. We can use this to add a book to our application and view the list of added books.

3.4. Other Features

When we open Book.java class file, we'll notice that the class is annotated with @Roo annotations. These were added by Roo Shell and are used to control and customize the content of AspectJ inter-type declaration (ITD) files. We can view the files in Package Explorer in STS by unselecting “Hide generated Spring Roo ITDs” filter in View Menu, or we can open the files directly from the file system.

Roo annotations have SOURCE retention policy. This means the annotations won't be present in compiled class bytecode, and there won't be any dependency to Roo in deployed applications.

Another, obviously missing part in the Book.java class are getters and setters. These are stored in separate AspectJ ITDs files, as already mentioned. Roo will actively maintain this boilerplate code for us. Therefore, changes to fields in any class will be automatically reflected in AspectJ ITDs as Roo is “monitoring” all changes – either done via Roo Shell or directly by a developer in IDE.

Roo will take care of the repetitive code like toString() or equals() methods too.

Moreover, the framework can be easily removed from the project, avoiding vendor lock-in, by removing annotations and pushing-in AspectJ ITD to standard java code.

4. Conclusion

In this quick example, we managed to install and configure Spring Roo in STS and created a small project.

We used Roo Shell to set it up and didn't need to write a single line of actual Java code! And we were able to get a working application prototype in a few minutes and Roo took care of all boilerplate code for us.

As always, the code used throughout the discussion can be found over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!