Skip to content

Commit 545e98a

Browse files
Copilotbrunoborges
andauthored
Add Spring Boot MVC configuration modernization pattern
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
1 parent e30c957 commit 545e98a

3 files changed

Lines changed: 81 additions & 1 deletion

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
id: 111
3+
slug: "spring-boot-mvc-config"
4+
title: "Spring Boot MVC Configuration"
5+
category: "enterprise"
6+
difficulty: "beginner"
7+
jdkVersion: "17"
8+
oldLabel: "Spring Boot 1.x"
9+
modernLabel: "Spring Boot 4.x"
10+
oldApproach: "WebMvcConfigurerAdapter with @EnableWebMvc"
11+
modernApproach: "WebMvcConfigurer with Boot Auto-Configuration"
12+
oldCode: |-
13+
@EnableWebMvc
14+
@Configuration
15+
public class WebConfig extends WebMvcConfigurerAdapter {
16+
@Override
17+
public void addViewControllers(
18+
ViewControllerRegistry registry) {
19+
registry.addViewController("/").setViewName("home");
20+
}
21+
}
22+
modernCode: |-
23+
@Configuration
24+
public class WebConfig implements WebMvcConfigurer {
25+
@Override
26+
public void addViewControllers(
27+
ViewControllerRegistry registry) {
28+
registry.addViewController("/").setViewName("home");
29+
}
30+
}
31+
summary: "Replace the removed WebMvcConfigurerAdapter with WebMvcConfigurer and retain Spring Boot's MVC auto-configuration."
32+
explanation: "Spring Boot 1.x applications commonly extended WebMvcConfigurerAdapter because Java interfaces could not provide default methods. Modern Spring Framework versions provide default implementations directly on WebMvcConfigurer, so implement the interface and override only the methods you need. In a Spring Boot application, do not add @EnableWebMvc unless you intentionally want to take full control of MVC configuration: it disables Boot's MVC auto-configuration, including its usual converters, static-resource handling, and other defaults."
33+
whyModernWins:
34+
- icon: "🧹"
35+
title: "No removed adapter"
36+
desc: "WebMvcConfigurer supplies default methods, so there is no adapter superclass to extend."
37+
- icon: "⚙️"
38+
title: "Keeps Boot defaults"
39+
desc: "Without @EnableWebMvc, Spring Boot continues to configure MVC infrastructure automatically."
40+
- icon: "🎯"
41+
title: "Override only what matters"
42+
desc: "Implement the interface and customize just the MVC hook required by the application."
43+
support:
44+
state: "available"
45+
description: "Available in Spring Boot 4.0 with Spring Framework 7.0 (requires Java 17+)"
46+
prev: "enterprise/spring-null-safety-jspecify"
47+
next: null
48+
related:
49+
- "enterprise/spring-api-versioning"
50+
- "enterprise/spring-xml-config-vs-annotations"
51+
- "enterprise/spring-null-safety-jspecify"
52+
tags:
53+
- spring
54+
- spring-boot
55+
- webmvc
56+
- migration
57+
docs:
58+
- title: "Spring Framework — MVC Java Configuration"
59+
href: "https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-config.html"
60+
- title: "Spring Boot — MVC Auto-configuration"
61+
href: "https://docs.spring.io/spring-boot/reference/web/servlet.html"

content/enterprise/spring-null-safety-jspecify.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ support:
7777
state: "available"
7878
description: "Available since Spring Framework 7.0 (requires Java 17+)"
7979
prev: "enterprise/jdbc-resultset-vs-jpa-criteria"
80-
next: null
80+
next: "enterprise/spring-boot-mvc-config"
8181
related:
8282
- "errors/helpful-npe"
8383
- "errors/optional-orelsethrow"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
//JAVA 25+
3+
//DEPS org.springframework:spring-webmvc:7.0.5
4+
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
7+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
8+
9+
/// Proof: spring-boot-mvc-config
10+
/// Source: content/enterprise/spring-boot-mvc-config.yaml
11+
@Configuration
12+
class WebConfig implements WebMvcConfigurer {
13+
@Override
14+
public void addViewControllers(ViewControllerRegistry registry) {
15+
registry.addViewController("/").setViewName("home");
16+
}
17+
}
18+
19+
void main() {}

0 commit comments

Comments
 (0)