In this tutorial we will learn how Spring Boot greatly simplify the creation of Web applications by using its auto-configuration, comparing with the old Spring approach.

The Spring Framework used as basis for developing Web applications the MVC (Model View Controller) pattern which greatly simplifies application development. Let’s have a look how you could build a standard Spring MVC application starting from a simple Maven archetype:

$ mvn archetype:create -DgroupId=com.example -DartifactId=simple-mvc-app -DarchetypeArtifactId=maven-archetype-webapp

After some adjustments to the pom.xml file we will have this project file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
      <!-- Generic properties -->
      <!-- Web -->
      <!-- Spring -->
      <!-- Spring MVC -->


As you can see, we are using Spring MVC version 3.2.3.RELEASE and some other dependencies like the tag libraries. Right now the Spring MVC is in its 5.x version. Next, let’s look at web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="" xmlns:xsi="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">

As you can see, we are using a Servlet declaration that adds the org.springframework. web.servlet.DispatcherServlet class that is the main dispatcher that will trigger the MVC pattern. As an additional declaration, the tag will look for an XML configuration file, in this case WEB-INF/mvc-config.xml . This file is a Spring context configuration.

Finally, take a look at the Spring configuration contained in src/main/webapp/WEB-INF/mvc-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:context="" xmlns:mvc="" xmlns:xsi="" xsi:schemaLocation="">
   <bean name="/showMessage.html" class="com.apress.spring.SimpleController" />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/view/" />
      <property name="suffix" value=".jsp" />

This is the standard Spring configuration where you define your bean that will be instantiate the Spring container. Within this configuration file, there is a bean named /showMessage.html that is pointing to the com.example.SimpleController Java class. This peculiar declaration is the URL that will map to the class to be executed when there is a request to the /showMessage.html URL. There is also one more bean declaration that you need to add to define your views. In this case, every view will placed in the /WEB-INF/view folder and every page will have the .jsp extension. Now let's take a look at the class:

package com.example;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class SimpleController extends AbstractController {
 protected ModelAndView handleRequestInternal(HttpServletRequest request,
  HttpServletResponse response) throws Exception {
  ModelAndView model = new ModelAndView("showMessage");
  model.addObject("message", "Spring MVC Web Application");
  return model;

This class extends the org.springframework.web.servlet.mvc.AbstractControlle class that has all the logic to manage your request ( /showMessage.html ). In the handleRequestInternal method we will return a ModelAndView instance that contains the information of what view to display.

Finally, let's compose the view, named "showMessage.jsp" as declared in the Controller class: (This file will be located in /WEB-INF/view/showMessage.jsp)

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
      <meta charset="utf-8" />

When you deploy this application, Spring will render the "Spring MVC Web Application" message.

Package your applicaiton as follows:

$ mvn clean package

This will create the simple-mvc-app.war application that can be deployed on a Web Server/Application server and tested with:

http://localhost:8080/simple-mvc-app/showMessage.html URL and it will show the message "Spring MVC Web Application".

Spring MVC 2.5 and above

In the above example, you might have noticed that we used an old approach to Spring MVC applications. As a matter of fact from the version 2.5 of Spring MVC you can add annotations to avoid extending from other classes, thus simplifying the mapping. Consider the following SimpleController class:

package com.example;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
public class HelloWorldController {
 @RequestMapping(method = RequestMethod.GET)
 public ModelAndView helloWorld() {
  ModelAndView model = new ModelAndView("showMessage");
  model.addObject("message", "Spring MVC Web App with annotations");
  return model;

The changes required in src/main/webapp/WEB-INF/mvc-config.xml are:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:context="" xmlns:mvc="" xmlns:xsi="" xsi:schemaLocation="">
   <context:component-scan base-package="com.example" />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/view/" />
      <property name="suffix" value=".jsp" />

As you can see from the above example, now we are using a tag which tells the Spring engine to scan for all classes marked with that package. In this example, it will find the SimpleController class as it has been tagged with the @Controller annotation. Also, you might have noticed that all bean definitions have been removed from the configuration that is now handled by the SimpleController class.

Spring Boot Web Applications

Now it's time to see how Spring Boot can help you in creating Web application using the MVC pattern. We will use the Spring CLI to bootstrap a simple Web application template therefore open a terminal, create a folder ( simple-mvc-spring-boot ), and execute the following command:

$ mkdir simple-mvc-spring-boot
$ cd simple-mvc-spring-boot
$ spring init -d=web -g=com.example -a=simple-mvc-spring-boot --package-name=com.example -name=simple-mvc-spring-boot -x

This command will create a template for your Spring Boot web application.

• The -d=web tells the CLI to include the web starter

• The -g=com.example is the groupId

• The -a=simple-mvc-spring-boot is the artifactId

• The --package-name=com.example is the base package name for the classes

• The -name=simple-mvc-spring-boot is the name of the application

• The -x will extract the template in the current directory; if you omit the extract option, you will find a file

Your file structure should be similar to the following one:

├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── example
    │   │           └──
    │   └── resources
    │       ├──
    │       ├── static
    │       └── templates
    └── test
        └── java
            └── com
                └── example

Now open the file and modify it to look the same as

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
public class SimpleMvcSpringBootApplication {
 public String index() {
  return "Hello World Spring Boot MVC!";
 public static void main(String[] args) {, args);

In this modified application we have marked the rest controller with the @RestController annotation and defined an index method marked with @RequestMapping . The purpose of this method is to accept all incoming requests to the /hello.html URL. To run it, just execute the following command:

$ ./mvnw spring-boot:run

This command will run the application, so check with the browser at the default Web server address and port for http://localhost:8080/hello.html URL. You will see the message: "Hello World Spring Boot MVC!".

Spring Boot without Maven

In the above example, we have created a Spring Boot application from a template, by invoking the "spring init" command. You can even create a Maven-less application which just uses a minimal @RequestController and @RequestMapping. By using groovy as language, you won't even need package declarations for the standard types:

class WebApp{
	String greetings(){
		"Spring Boot MVC is easier"

You can just run the application with the following command:

$ spring run app.groovy

Now check with the browser at: http://localhost:8080/hello.html . As you can see we got rid of maven, web.xml, bean declaration and any kind of configuration


In this tutorial we have seen the evolution of Spring MVC applications and how Spring Boot greatly simplifies the design and development of Web applications using this awesome pattern. Check out for more Spring Boot MVC tutorials:

REST services in Spring Boot with MVC and JAX-RS

Spring Boot MVC example

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials