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 \
    -DarchetypeVersion=1.0.2
    

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

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 -Dspring-boot.run.arguments=--program=automobile
    

These commands use a different value for the program variable.

mvn spring-boot:run -Dspring-boot.run.arguments=--program=client

mvn spring-boot:run -Dspring-boot.run.arguments=--program=none

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
                    <artifactId>boot-web</artifactId>
    2. Change the name to boot-web
                    <name>boot-web</name>
    3. Add the dependency for the starter-web to the pom file.
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
    4. Add the dependency for the starter-tomcat to the pom file.
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-tomcat</artifactId>
              </dependency>
  3. Edit the SimpleBean.java file in the com/bytesizebook folder.
    1. Remove all the packages and contents except for com.bytesizebook.com. 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.
      @SpringBootApplication
      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.
      @SpringBootApplication
      public class SimpleBean extends SpringBootServletInitializer {    
          
          public static void main(String[] args) throws Exception {
              SpringApplication.run(SimpleBean.class, 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 Index.java in the com/bytesizebook folder. This is the same folder as the one that contains the SimpleBean.java file.
    2. Modify the class with the @Controller annotation.
      @Controller
      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>
      <html>
          <head>
              <title>Spring Boot Application</title>
              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          </head>
          <body>
              <h1>Spring Boot Application</h1>
          </body>
      </html></pre>
        
  6. Additional resources for the application belong in the src/main/resources folder.
    1. Create the resources folder.
    2. Create a new file named application.properties 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 context.name. 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
      context.name=/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:
    http://localhost:8080/boot-web/
    Be sure to include the final / or the page will not be found.

Contact the author