Understanding the Relationship Between Spring Boot and Spring MVC and Their Configuration Features

This article explains how Spring Boot, Spring, and Spring MVC relate to each other, describes Spring MVC's core concepts, and details Spring Boot's auto‑configuration, static resource handling, error handling, CORS support, and related code examples for building Java backend applications.

Java Captain
Java Captain
Java Captain
Understanding the Relationship Between Spring Boot and Spring MVC and Their Configuration Features

Spring Boot is a large framework that bundles many components, with Spring as the core and Spring MVC as the web‑layer module. Their relationship can be expressed as Spring MVC < Spring < Spring Boot.

Clarifying the Relationship Between Spring Boot and Spring MVC

The Spring family includes many derivatives such as Boot, Security, JPA, etc., all built on Spring's IoC container and AOP support, which provide dependency injection and cross‑cutting concerns.

Spring MVC is a Servlet‑based MVC framework that solves web‑development problems, but its configuration can be cumbersome (XML, JavaConfig, etc.). Spring Boot was created to simplify this by adopting "convention over configuration" and reducing the amount of boilerplate configuration.

In short:

Spring is an "engine".

Spring MVC is a web MVC framework built on Spring.

Spring Boot is a set of starter packages that provide conditional auto‑registration for rapid development on top of Spring.

Spring MVC Auto‑Configuration

Spring Boot provides auto‑configuration for Spring MVC that adds the following features for most applications:

Registration of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

Support for static resources, including WebJars.

Automatic registration of Converter, GenericConverter, and Formatter beans.

Support for HttpMessageConverters.

Automatic registration of MessageCodesResolver.

Support for a static index.html.

Support for a custom favicon.

Automatic use of ConfigurableWebBindingInitializer bean.

If you keep Spring Boot's MVC features, you can add your own MVC configuration (interceptors, formatters, view controllers, etc.) by defining a @Configuration class that implements WebMvcConfigurer without needing @EnableWebMvc. To take full control, add @EnableWebMvc on a configuration class.

HttpMessageConverters

Spring MVC uses the HttpMessageConverter interface to convert HTTP requests and responses. By default, JSON conversion uses Jackson and XML conversion uses JAXB (if Jackson XML is present). You can customize converters with Spring Boot's HttpMessageConverters class:

import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.*;
import org.springframework.http.converter.*;

@Configuration
public class MyConfiguration {

    @Bean
    public HttpMessageConverters customConverters() {
        HttpMessageConverter<?> additional = ...
        HttpMessageConverter<?> another = ...
        return new HttpMessageConverters(additional, another);
    }
}

All HttpMessageConverter beans in the context are added to the converters list, allowing you to replace the default set.

Static Content

By default Spring Boot serves static resources from classpath:/static, /public, /resources, or /META-INF/resources. You can customize locations with spring.resources.staticLocations. Spring Boot also supports WebJars and provides cache‑busting strategies:

spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

For a fixed version strategy:

spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
spring.resources.chain.strategy.fixed.enabled=true
spring.resources.chain.strategy.fixed.paths=/js/lib/
spring.resources.chain.strategy.fixed.version=v12

Welcome Page

Spring Boot first looks for an index.html in the configured static locations; if none is found, it looks for an index template. The found file becomes the application's welcome page.

Custom Error Pages

Spring Boot registers a global /error mapping that returns JSON for machine clients and a whitelabel HTML view for browsers. You can replace the default behavior by providing an ErrorController bean or an ErrorAttributes bean.

To provide a custom HTML error page for a specific status code, place a file named 404.html (or other status) under src/main/resources/public/error:

src/
 └─ main/
     ├─ java/
     │   └─ <source code>
     └─ resources/
         └─ public/
             └─ error/
                 └─ 404.html

For template‑based error pages (e.g., FreeMarker), place 5xx.ftl under src/main/resources/templates/error:

src/
 └─ main/
     ├─ java/
     │   └─ <source code>
     └─ resources/
         └─ templates/
             └─ error/
                 └─ 5xx.ftl

You can also implement ErrorViewResolver to provide programmatic error views:

public class MyErrorViewResolver implements ErrorViewResolver {

    @Override
    public ModelAndView resolveErrorView(HttpServletRequest request,
                                         HttpStatus status, Map<String, Object> model) {
        // Use the request or status to optionally return a ModelAndView
        return ...
    }
}

Non‑MVC Error Handling

For applications that do not use Spring MVC, you can register error pages directly with ErrorPageRegistrar:

@Bean
public ErrorPageRegistrar errorPageRegistrar() {
    return new MyErrorPageRegistrar();
}

private static class MyErrorPageRegistrar implements ErrorPageRegistrar {
    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        registry.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
    }
}

If the error page is processed by a filter, ensure the filter is registered for DispatcherType.ERROR:

@Bean
public FilterRegistrationBean myFilter() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(new MyFilter());
    // ...
    registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
    return registration;
}

CORS Support

Since Spring MVC 4.2, CORS is supported out of the box. You can enable it per controller with @CrossOrigin or globally by defining a WebMvcConfigurer bean:

@Configuration
public class MyConfiguration {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/api/**");
            }
        };
    }
}

Content Negotiation

Spring Boot disables suffix‑based path matching by default. You can enable it or configure parameter‑based negotiation:

spring.mvc.contentnegotiation.favor-parameter=true
# change the parameter name (default is "format")
# spring.mvc.contentnegotiation.parameter-name=myparam
# register additional extensions/media types
spring.mvc.contentnegotiation.media-types.markdown=text/markdown

To re‑enable suffix matching:

spring.mvc.contentnegotiation.favor-path-extension=true
# limit to known extensions
# spring.mvc.pathmatch.use-registered-suffix-pattern=true
# register additional extensions/media types
spring.mvc.contentnegotiation.media-types.adoc=text/asciidoc

Spring HATEOAS

If you develop hypermedia‑driven REST APIs, Spring HATEOAS can be auto‑configured by Spring Boot, replacing the need for @EnableHypermediaSupport and providing necessary beans such as LinkDiscoverers and a customizable ObjectMapper.

Additional Topics

The article also touches on error handling for WebSphere, template engine auto‑configuration (FreeMarker, Groovy, Thymeleaf, Velocity, Mustache), and best practices for packaging static resources in JAR versus WAR deployments.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javabackend-developmentConfigurationSpring BootWeb DevelopmentSpring MVC
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.