• Creating Simple Spring Boot Web Application Using Maven

    In this tutorial, we will show you how to create a very simple spring boot web application using maven.

    Spring Boot lets you create an application with minimal configurations but with the functionality of and advantages of spring framework. When creating a simple spring boot web application, spring boot eliminates the need of creating a web.xml, a dispatcher or other xml configurations. Spring Boot automatically create those configurations for you just by adding the right dependency in your maven or gradle project.

    Creating Simple Spring Boot Web Application Using Maven

    This uses spring mvc, thymeleaf and of course the spring framework. This can be your based project when creating a web application in the future.

    1. Create a New Project with Maven

      First, we create a new project in IDE. We are using IntelliJ but should be the same in other IDEs.

      IntelliJ New Project In Maven

      new-project-maven


      You don’t need to create a maven project using any archetype. We’ll just create the basic maven project.
    2. Add Pom Dependencies

      Next we configure the pom.xml. First, add the packaging tag to tell maven how to package your application. First, lets make this to jar. We’ll also show you how to package this into war file to be deployed in application servers.

      <packaging>jar</packaging>
      

      To be able for your project to work with spring boot, your project should be a child of the parent spring boot module. So we need to add the parent tag in our pom.xml and point to spring-boot-starter-parent.

      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>1.3.6.RELEASE</version>
          <relativePath/> 
      </parent>
      

      Then we need the spring boot starter dependencies.

          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-thymeleaf</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-devtools</artifactId>
              </dependency>
          </dependencies>
      
      • spring-boot-starter – this lets spring boot autoconfigure your application
      • spring-boot-starter-web – this dependency tells spring boot that your application is a web application. This add the functionality of @Controller, @RequestMapping, etc.
      • spring-boot-starter-thymeleaf – web app needs some views like jsp or html. Thymeleaf is a server side template engine where your htmls can be viewed in any browser. Simply to say, instead of traditional jsp with jstl or format tags, we will use thymeleaf.
      • spring-boot-devtools – this is an optional dependency but this makes your development faster. This dependency automatically restart or hotswap your changes to the running tomcat server, thus removing the need to restart the application whenever there are changes in codes.
    3. Create the Main Class

      Just like any java application, we need to define the main method in which will be the starting point. Here we create a class named Application.java with the annotation @SpringBootApplication.

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

      This will also be the file that you should run in order to start your web application.

    4. Spring MVC Controllers

      To be able to handle web request, our application should have controllers. Here is a sample controller HomeController that handles the request to the path “/”.

      @Controller
      public class HomeController {
      
          @RequestMapping("/")
          public String viewHome() {
              return "index";
          }
      }
      

      The method viewHome() tell us that any request for path “/” will go inside this method and will return the view “index” which will be created in the next step.

    5. Creating the Views

      Thymeleaf needs 2 folder: static and templates folder inside resources folder. The static folder contains your static files like css, js and images. The templates folder contains the actual htmls that will be use to create the html that will be rendered on browsers.

      thymeleaf directory structure

      thymeleaf directory structure


      Our index.html contains a basic form login.
    6. Creating application.properties

      At this time, you can already run your project without creating application.properties file. Spring will just use the default configurations for your web application. If you want to be more specific, you can create application.properties inside resources folder and define the configuration that you want to use such as the context path, port of tomcat to use and more (see Spring Boot Commong Application Properties).
      Here is a sample application.properties:

      server.contextPath=/javapointers
      server.port=8085
      

      This tell us that the application will have a context path of “/javapointers” and tomcat port of 8085 so the final url will be localhost:8085/javapointers.

    7. Running the Web Application

      Finally, we can now run our simple spring boot web application. To run, open the class Application then right click the main method and select run or debug to debug the app. This will display the spring boot banner and logs in the console.

      In your favorite browser, type “localhost:” + the port you have chosen + the context path. In our case, “localhost:8085/javapointers”. Then it should show the html that you have created:
      user-login

    You can download the source code for simple spring boot web application using this link

    Next tutorial is about how you can Add Spring Security to Spring Boot Web Application.

    Related Post

    • WhoDidWhatNow?

      This was / is pretty half-assed. Put some effort into writing a tutorial if you’re going to do it.