Chapter 4 Tutorials

Command Line Application

The starter code for this chapter has been moved to the central repository. The new coordinates are different that the ones listed in the book. Always take the latest version of the code. The current version is 1.0.2. The only difference from the book's coordinates is the version:

  • Group ID: com.bytesizebook
  • Artifact ID: spring-boot-java-cli
  • Version: 1.0.2

Use the coordinates of this archetype to generate the starting code, as in Chapter 1. This is one command that appears on one line. The \ character is used to escape the newline character so it can be displayed in the book.

mvn archetype:generate \
    -DgroupId=com.bytesizebook \
    -DartifactId=spring-cli \
    -DpackageName=com.bytesizebook \
    -DarchetypeGroupId=com.bytesizebook \
    -DarchetypeArtifactId=spring-boot-java-cli \

The exact values for the archetype can be seen by searching for com.bytesizebook in

If the archetype is not selected automatically and you are asked to enter a filter, enter spring-boot-java-cli and select the one for bytesizebook.

Once the archetype is generated, run it with the following command. Unfortunately, the typesetter for the book changed -- to - in the text. The correct command is here:

mvn spring-boot:run

These commands use a different value for the program variable.

mvn spring-boot:run

mvn spring-boot:run

Spring Boot MVC Application

  1. Copy the command line application above to a new folder.
  2. Edit the pom.xml.
    1. Change the artifact ID to boot-web
    2. Change the name to boot-web
    3. Add the dependency for the starter-web to the pom file.
    4. Add the dependency for the starter-tomcat to the pom file.
  3. Edit the file in the com/bytesizebook folder.
    1. Remove all the packages and contents except for They contain the beans that were used in the command line application and are not needed anymore.
    2. Remove the implements clause and extend the class from SpringBootServletInitializer. This is only needed to generate WAR files for deployment.
      public class SimpleBean extends SpringBootServletInitializer { 
    3. Keep the main method and remove all the other methods and variables from the previous application. Only the main method is needed for this application.
      public class SimpleBean extends SpringBootServletInitializer {    
          public static void main(String[] args) throws Exception {
    , args);
  4. Java source files belong in the src/main/java folder. An IDE might create an alias for the folder. Netbeans uses the alias Source Packages.
    1. Create a Java file named in the com/bytesizebook folder. This is the same folder as the one that contains the file.
    2. Modify the class with the @Controller annotation.
      public class Index {
  5. Public web pages for the application belong in the src/main/webapp folder.
    1. Create the webapp folder in src/main folder.
    2. Create a file named index.html with the following text:
      <!DOCTYPE html>
              <title>Spring Boot Application</title>
              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
              <h1>Spring Boot Application</h1>
  6. Additional resources for the application belong in the src/main/resources folder.
    1. Create the resources folder.
    2. Create a new file named in the resources folder. This file can be used to define properties that change the behavior of the application.
    3. By default, all the files in this web application will start from the root URL of the web server. To use a name that starts all URLs for this web application, create a property for All resources from this web application will have this name as a preface to its URL. To agree with the URLs that are listed in the book, set the name to /boot-web
  7. Start the application with the maven command:
    mvn spring-boot:run
  8. Access the application through a web browser with the URL:
    Be sure to include the final / or the page will not be found.

Contact the author