Spring Boot ViewControllerRegistry tutorial

Spring Boot ViewControllerRegistry tutorial shows how to use ViewControllerRegistry to create simple routes.

Spring is a popular Java application framework for creating enterprise applications. Spring Boot is an evolution of Spring framework which helps create stand-alone, production-grade Spring based applications with minimal effort.


ViewControllerRegistry allows to create simple automated controllers pre-configured with status code and/or a view.

Spring Boot ViewControllerRegistry example

In the following example we create a simple route with ViewControllerRegistry.

│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           └───config
│   │                   AppConfig.java
│   └───resources
│       ├───static
│       │       index.html
│       └───templates
│               hello.html

This is the project structure.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"










Spring Boot starters are a set of convenient dependency descriptors which greatly simplify Maven configuration. The spring-boot-starter-parent has some common configurations for a Spring Boot application. The spring-boot-starter-web is a starter for building web, including RESTful, applications using Spring MVC. It uses Tomcat as the default embedded container. The spring-boot-starter-thymeleaf is a starter for building MVC web applications using Thymeleaf views.

The spring-boot-maven-plugin provides Spring Boot support in Maven, allowing us to package executable JAR or WAR archives. Its spring-boot:run goal runs the Spring Boot application.

package com.zetcode.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class AppConfig implements WebMvcConfigurer {

    public void addViewControllers(ViewControllerRegistry registry) {

In the AppConfig we register a new route with ViewControllerRegistry's addViewController() method.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Hello page</title>

    Hello there


The hello.html view displays a simple message.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Home page</title>
    This is home page. Go to <a href="hello">hello page</a>


This is a home page.

package com.zetcode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class Application {

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

The Application sets up the Spring Boot application. The @SpringBootApplication enables auto-configuration and component scanning.

$ mvn spring-boot:run 

After the application is run, we can navigate to localhost:8080/.

In this tutorial, we have showed how to use Spring ViewControllerRegistry to create simple routes.