Setting up a basic Spring Boot web application with Maven

In this tutorial I am going to show you, how to set up a basic web application using Spring Boot and Maven. The web application will have a single “home” page, rendered using a spring controller and a jsp file.


Technologies used in this tutorial:

  • Apache Tomcat 8.0.35
  • Apache Maven 3.3.9
  • Java 1.8.0_60

The structure of the project

Here you can see the file structure of the completed project:


Setting up the POM file

A basic Spring Boot web application requires very little configuration in the POM. We only have to include one dependency (apart from the servlet api needed for using jsp views). This is the spring-boot-starter-web, that transitively contains all other dependencies that are required for the spring controllers, logging etc.

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
    <name>Setting up a basic Spring Boot web application with Maven</name>


        <!-- Dependency versions -->


        <!-- Plugin versions -->


        <!-- Misc versions -->




        <!-- Provided dependencies -->




In the plugin configuration there is nothing Boot specific. We just set the Java version using the maven-compiler-plugin and also set the failOnMissingWebXml to false, because we deliberately don’t have a web.xml file.

The application runner class

A spring boot application can be initialized by a very simple class containing a main method.

package com.jtuts.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "com.jtuts")
public class Application extends SpringBootServletInitializer {

    public static void main(final String[] args) {, args);


Note, that we have the @SpringBootApplication annotation, which is actually is a shorthand for the following ones:

  • @Configuration marks the class as a source of bean definitions for the application context.
  • @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
  • @EnableWebMvc is automatically added when Boot sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
  • @ComponentScan tells Spring to look for other components, configurations.

The  @Component annotation is automatically added, but by default it only looks for components in the current package, so we needed to override it, to look for components in the parent package. This way, it’ll see our controller also.

Extending the SpringBootServletInitializer is required to be able to deploy the application as a war file to a container (e.g. Tomcat). It is actually an implementation of the WebApplicationInitializer interface, that you would use when you want to create a web application without Boot.

That’s it for the application startup.

Creating the controller

Our controller is plain and simple, just returns a view without passing any parameters.

package com.jtuts.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

public class HomeController {

    private static final String HOME = "home";

    @RequestMapping(value = HOME)
    public ModelAndView home() {
        return new ModelAndView(HOME);

For this controller to work, we also have to specify a view resolver in a configuration class, that tells Spring where to find the view files based on their names.

package com.jtuts.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

public class ViewConfiguration {

    private static final String VIEW_PATH = "/WEB-INF/views/";
    private static final String JSP_EXTENSION = ".jsp";

    public ViewResolver setupViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();


        return resolver;


Our view file

The view file is just a really simple one:

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Home page</title>
    <h1>Welcome to the home page.</h1>

How to launch the app

To launch this application, you just have to copy the war file created during build to a Tomcat container.

Additional notes

As you can see, a logback.xml is also included. This is because Spring Boot by default expects the logback configuration and the app would fail to start without that. The exact logging configuration is not really relevant to this tutorial, but anyway, here is the content of that file:

<?xml version="1.0" encoding="UTF-8"?>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>


You can download the working example project here.