Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Generate Spring Boot Applications Using Spring Roo

DZone's Guide to

How to Generate Spring Boot Applications Using Spring Roo

Do you know the new Spring Roo 2.0.0.M2 features? Do you want to generate complete Spring Boot applications quickly? Read on to learn more!

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

Hi all!

Many of you have been asked about tutorials and guides about how to use the new Spring Roo 2.0.0.M2 version and how to generate Spring Boot applications with it. For those who don’t know what the Spring Roo project is, I’ll try to explain it really quick:

Spring Roo is a development tool that provides all the necessary features to generate web applications quickly and easily. I can be used from STS (based on Eclipse IDE) or directly from your OS shell (Linux, Windows and OSx). Developers can execute some predefined commands that generates all the Java code you need to have a complete web application.

This new version (2.0.0.M2) of Spring Roo includes some new features like:

  • Using the Spring IO Platform to manage dependency versions and be able to use the latest Spring technologies.
  • Update code generation to include Spring Boot on generated projects.
  • Multi-module projects improvement.
  • Push-in operations using Spring Roo Shell.
  • Different response types are allowed in the same controller (JSON, THYMELEAF, etc…)
  • Views can be generated using THYMELEAF.
  • Dojo has been replaced with HTML5, CSS3, Bootstrap, and jQuery components. (Datatables, Select2, etc…)
  • … and much more!

To get more details about all the new Spring Roo 2.0.0.M2 features, see the release note posted on the Spring Blog. If you are interested on get more information about the Spring Roo project visit its official project page.

Now that everyone knows what Spring Roo is, let’s start with this quick tutorial.

Introduction

In this quick tutorial we are going to generate a basic Spring Boot application using Spring Roo 2.0.0.M2 from OS shell. Don’t worry if you are not familiar with the OS shell, this will be a really easy tutorial explained step by step. Also, in future tutorials I’ll explain to you how to configure your STS to include the Spring Roo shell.

The generated application will contain:

  • Basic Spring Boot project structure and the Spring IO platform to manage dependencies and Spring Boot starters.
  • JPA Persistence using HIBERNATE and HYPERSONIC_IN_MEMORY database.
  • One entity called Owner with a username and email field.
  • Spring Data JPA repository to manage the Owner entity.
  • Service interface and its implementation to manage the Owner entity.
  • Controller to provide necessary operations to manage the Owner entity.
  • THYMELEAF views.

Remember that this will be a quick tutorial to show you how to generate Spring Boot applications using Spring Roo shell, so I’m not going to go deeper on the other components of the generated project (persistence, view layer, etc.). Just follow this tutorial to have a complete Spring Boot application and in future posts, I’ll explain you all about the other generated layers.

Prerequisites

Executing Spring Roo 2.0.0.M2

  • Download Spring Roo 2.0.0.M2.
  • Unzip the downloaded distribution. For example, inside ~/software folder.
  • Open a new OS shell on the folder where you want to generate your new project and execute the ~/software/spring-roo-2.0.0.M2/bin/roo.sh script file (or use .bat file if you are working on Windows)
  • New Spring Roo 2.0.0.M2 will be executed with this appearance:

Shell started

Create a New Spring Boot Project

In this new version, Spring Roo generates Spring Boot applications by default. So, by executing the following command in the Spring Roo shell, a new Spring Boot project will be generated (remember that you can use TAB to autocomplete commands):

project setup --topLevelPackage com.disid.blog.tutorial

Image title

As you can see in the shell log, the following elements have been generated:

Created ROOT/pom.xml
Created SRC_MAIN_RESOURCES
Created SRC_MAIN_JAVA/com/disid/blog/tutorial
Created SRC_MAIN_JAVA/com/disid/blog/tutorial/TutorialApplication.java
Created SRC_MAIN_RESOURCES/banner.txt


The basic structure of the generated project should look like:

.
├── log.roo
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── disid
        │           └── blog
        │               └── tutorial
        │                   └── TutorialApplication.java
        └── resources
            └── banner.txt


The log.roo file contains all the executed commands in the Spring Roo shell. It will be updated everytime that you execute a new command that ends successfully. It’s really useful to know which commands have been executed before.

The pom.xml contains minimum dependencies and minimum Spring Boot starters that will be used in the generated Spring Boot project. To get more info about existing Spring Boot starters, you should check the Spring Boot documentation.

Also, if you open this file, you should check that the parent of the generated project will be the Spring IO Platform. You can learn more about it reading my post about how to use the Spring IO Platform and Spring Boot together.

The TutorialApplication.java file is the class that contains the main method. It’s annotated with @SpringBootApplication and looks like this:

@SpringBootApplication
public class TutorialApplication {

    public static void main(String[] args) {
        SpringApplication.run(TutorialApplication.class, args);
    }
}

The @SpringBootApplication annotation is equivalent to using @Configuration@EnableAutoconfiguration, and @ComponentScan annotations with their default attributes. To know more about the @SpringBootApplication annotation, read the Spring Boot documentation.

Finally, banner.txt file has been generated on resources folder. This banner will be displayed during application deployment. The appearance of the generated file should look like:

${AnsiColor.GREEN}                _
 ___ _ __  _ __(_)_ __   __ _ _ __ ___   ___
/ __| '_ \| '__| | '_ \ / _` | '__/ _ \ / _ \
\__ \ |_) | |  | | | | | (_| | | | (_) | (_) |
|___/ .__/|_|  |_|_| |_|\__, |_|  \___/ \___/
    |_|                 |___/

${AnsiColor.BRIGHT_RED}Spring application made with Spring Roo 2.0
Doubts? http://projects.spring.io/spring-roo/${AnsiColor.DEFAULT}


Now, we have a basic Spring Boot application that compiles without any problem using the Maven command mvn clean compile in our system shell (not in Spring Roo shell!!). But… we are not able to run this application because we haven't configured the presentation layer.

Remember that this tutorial was only a quick guide about how to generate a basic Spring Boot application using Spring Roo 2.0.0.M2 (this section). However, I’m going to give you some quick commands that will allow you to get a complete Spring Boot application with web layer.

Persistence Configuration

To be able to generate persistence using HIBERNATE and HYPERSONIC_IN_MEMORY database, you should execute the following command:

jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY


Create a New Entity and its Fields

Create a new entity called Owner and include the username and the email fields. You will be able to do this by executing the following commands:

entity jpa --class ~.domain.Owner
field string --fieldName username
field string --fieldName email


Generate the Spring Data JPA Repository

Generate the Spring Data JPA repository to manage the Owner data access by executing the following command:

repository jpa --entity ~.domain.Owner --interface ~.repository.OwnerRepository


Generate the Service Layer

Generate the service layer by executing the following command:

service --entity ~.domain.Owner --repository ~.repository.OwnerRepository --interface ~.service.api.OwnerService --class ~.service.impl.OwnerServiceImpl


Generate the Web Layer

Include Spring MVC support, install THYMELEAF resources and generate the controllers executing the following commands:

web mvc setup
web mvc view setup --type THYMELEAF
web mvc controller --controller ~.web.OwnerController --entity ~.domain.Owner --service ~.service.api.OwnerService --responseType THYMELEAF


Running Generated Applications

After executing all the commands above (don’t worry if you don’t understand some command or the generated code. I’ll explain it you in the next tutorial! :D), you should have a complete Spring Boot application.

If you want to run your generated application, you could do it by executing the following Maven command on your system shell (not in Spring Roo shell!!):

mvn clean compile spring-boot:run

After some log messages in your system shell, your application will be deployed on an embedded Tomcat server. You should have access to http://localhost:8080/ link and see your generated application running:

Home pageCreate new owner pageList all owners page

What do you think about it?! Do you like it? We only had to execute some simple commands! :D

NOTE: The previous Maven command is available because the generated pom.xml file includes the spring-boot-maven-plugin.

Conclusions

  • Spring Roo is a development tool that provides you simple commands to generate applications.
  • Spring Roo could be executed directly on system shell or from STS.
  • Since 2.0.0.M2 version, Spring Roo generates complete Spring Boot applications.
  • Spring Roo includes Spring Boot starters on generated project that makes generated code more simple and clear. Without excesive configuration clases or files.
  • Spring Roo includes Spring IO Platform as parent of the generated project.
  • With some simple commands and in few minutes, you could have a complete Spring Boot application with complete web layer that uses THYMELEAF, HTML5, Bootstrap and jQuery components.
  • Run your generated application using mvn clean compile spring-boot:run maven command.

Hope you enjoy with this new post and you learn more about the Spring Roo project and its new features!

If you have some comment or question, feel free to make it. See you soon!

Original Author: Juan Carlos García, Software Architect at DISID

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:
java ,spring boot ,spring roo ,spring

Published at DZone with permission of Pilar Argudo. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}