Skip to content

fix(spring): skip root forwarding when Vaadin servlet is not registered - #27245

Open
totally-not-ai[bot] wants to merge 5 commits into
mainfrom
fix/spring-root-mapping-without-vaadin-servlet
Open

totally-not-ai[bot] wants to merge 5 commits into
mainfrom
fix/spring-root-mapping-without-vaadin-servlet

Conversation

@totally-not-ai

Copy link
Copy Markdown
Contributor

Fixes #27244

behavior deviation · vaadin-spring · plain Spring apps that register the Vaadin servlet themselves and don't set vaadin.urlMapping

When vaadin.urlMapping isn't set, VaadinServletConfiguration assumes the Vaadin servlet is root-mapped and adds a catch-all mapping to every DispatcherServlet that forwards unmatched requests to a servlet named springServlet. An app that registers its own Vaadin servlet under another name and its own DispatcherServlet (e.g. at /api/*) got a 500 "No servlet with name 'springServlet'" for every request no controller handles, instead of a 404. The catch-all mapping now steps aside when no servlet with that name is registered.

Risks:

  • ✅ Setups where Vaadin registers the servlet itself (Spring Boot, VaadinMVCWebAppInitializer) keep forwarding: the servlet exists there. Apps that replace the vaadinForwardingController bean keep the old behavior.

  • RootExcludeHandler returns no handler when the default forwarding controller is used and the servlet context has no named dispatcher for its servlet name — exactly the case in which the forward would throw.

  • The default forwarding controller is now a private subclass of ServletForwardingController that remembers the servlet name, since the Spring class has no getter for it.

  • Chose this over also requiring the DispatcherServlet to be mapped to /: the servlet check covers the reported failure without guessing at other mappings.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Type of change

Bug fix

How to test

  • New unit tests: with the Vaadin servlet registered, unmatched requests are still forwarded to it; without it, the root mapping returns no handler, so the DispatcherServlet answers 404; with a custom forwarding controller, forwarding is unchanged.
  • Manually: plain Spring app with @EnableVaadin, own SpringServlet at /gui/* under another name, own DispatcherServlet at /api/*, no vaadin.urlMapping. /api/does-not-exist now gives 404 instead of 500.

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 1 656 files  +1   1 747 suites  +1   1h 40m 30s ⏱️ - 6m 55s
14 057 tests +3  13 985 ✅ +3  72 💤 ±0  0 ❌ ±0 
14 483 runs  +3  14 411 ✅ +3  72 💤 ±0  0 ❌ ±0 

Results for commit 05c5a46. ± Comparison against base commit 099ac3b.

♻️ This comment has been updated with latest results.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Fixed all five in 6f9e371: the fields of RootExcludeHandler are now final (including the new forwardingServletName, set with a ternary instead of a conditional assignment), and the private VaadinForwardingController is final. The four older fields were included because Sonar flagged them on this PR; none of them is reassigned, and they are private, so subclasses are unaffected. No behavior change.

@Artur-
Artur- requested review from vaadin-review-bot and removed request for vaadin-review-bot December 1, 2026 17:06
@Artur-
Artur- marked this pull request as ready for review December 1, 2026 17:06

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the changes — left 1 comment.

Finding
📝 Javadoc claims self-registration of the Vaadin servlet is what triggers the new skip, which is not what the code does.

* application excluded and the requests a static resource handler answers.
* <p>
* When the default forwarding controller is used and the servlet context
* has no servlet under the name it forwards to, which is the case when the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Javadoc claims self-registration of the Vaadin servlet is what triggers the new skip, which is not what the code does.

The clause "which is the case when the application registers the Vaadin servlet itself" states an equivalence the code does not have. Both supported self-registration paths — VaadinMVCWebAppInitializer and SpringBootAutoConfiguration.configureServletRegistrationBean — register under springServlet and keep forwarding, and even a hand-built ServletRegistrationBean<SpringServlet> gets that name deduced. The actual condition is "no servlet registered under the name the controller forwards to", which the sentence already says; the added clause only misleads a reader into thinking any self-registration turns Vaadin root routing off.

VaadinServletConfiguration.java:117 · documentation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, that clause overstated it. I removed it, so the Javadoc now states only the real condition: the mapping handles no requests when the default forwarding controller is used and no servlet is registered under the name it forwards to.

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Nothing to flag — the changes look good.

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Nothing to flag — the changes look good.

@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

@Artur- Artur- left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the logic for registering the vaadin servlet? Isn't the forwarding related to that and we should only register it when the vaadin servlet is auto registered for /* ?

@totally-not-ai

totally-not-ai Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

@Artur- The servlet is registered in two places, and VaadinServletConfiguration is only gated on vaadin.urlMapping being unset or /*:

  • Spring Boot: SpringBootAutoConfiguration.servletRegistrationBean registers springServlet at /vaadinServlet/* when the mapping is root. It is @ConditionalOnMissingBean, and an app that brings its own ServletRegistrationBean<SpringServlet> through the public configureServletRegistrationBean gets the same name and mapping, so it needs the forwarding too even though Vaadin didn't auto-register anything.
  • Plain Spring: VaadinMVCWebAppInitializer registers springServlet at /vaadinServlet/* plus a DispatcherServlet at /*. It does so after context.refresh(), so a condition evaluated while the context is built can't see that servlet yet.

The configuration also reaches plain Spring apps through @EnableVaadin, which imports it. That is the reported case: the app registers the servlet itself, under another name and mapping, and the forwarding has nothing to forward to.

So yes, the forwarding belongs to the root-mapped servlet. The PR checks that at request time: it forwards only when a servlet named springServlet exists in the servlet context. That covers both registration paths and the custom ServletRegistrationBean case without having to detect who registered it.

Gating the registration itself would also work, but needs more: in plain Spring the initializer would have to read the mapping before refresh() and mark the context, @EnableVaadin would stop bringing in the root mapping (a behavior change for an app that registers its own springServlet at /vaadinServlet/* the same way the initializer does), and on Boot the condition would have to recognise user-provided ServletRegistrationBeans. I kept the runtime check for that reason, but I can switch to registration-time gating if you prefer it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

2 participants