Skip to content

Fix for GH-2509: WebMvcLinkBuilder.linkTo() ignores basePath for @RepositoryRestController - #2612

Open
ruthst00 wants to merge 2 commits into
spring-projects:mainfrom
ruthst00:issue/2509
Open

ruthst00 wants to merge 2 commits into
spring-projects:mainfrom
ruthst00:issue/2509

Conversation

@ruthst00

@ruthst00 ruthst00 commented Sep 18, 2026

Copy link
Copy Markdown

Fixes 2509

What was implemented

1. BasePathAwareWebMvcLinkBuilderFactory.java (new)

A WebMvcLinkBuilderFactory subclass that overrides linkTo(Object invocationValue). When the target controller is annotated with @BasePathAwareController (which @RepositoryRestController is a meta-annotation of), it intercepts the WebHandler.linkTo() call and provides a custom finisher function that prepends the configured basePath to the mapping path before building the UriComponentsBuilder. It uses reflection to access the package-private WebMvcLinkBuilder(UriComponents, TemplateVariables, List<Affordance>) constructor so the returned type is still a proper WebMvcLinkBuilder.

2. RepositoryRestMvcConfiguration.java (modified)

Registers BasePathAwareWebMvcLinkBuilderFactory as a @Bean named basePathAwareWebMvcLinkBuilderFactory, injecting the RepositoryRestConfiguration. Added @author Steve Rutherford to the class Javadoc.

3. BasePathAwareWebMvcLinkBuilderFactoryUnitTests.java (new)

5 unit tests covering:

  • @BasePathAwareController gets base path prepended ✅
  • @RepositoryRestController gets base path prepended ✅
  • Regular (non-SDR) controllers are not affected ✅
  • Empty base path is a no-op ✅
  • Base path is not double-prepended ✅

  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

Signed-off-by: ruthes00 <ruthes00@gmail.com>
…yRestController ignores basePath

Introduce BasePathAwareWebMvcLinkBuilderFactory, a WebMvcLinkBuilderFactory
subclass that detects controllers annotated with @BasePathAwareController
(which includes @RepositoryRestController) and automatically prepends the
configured spring.data.rest.base-path to the generated URI.

Previously, calling WebMvcLinkBuilder.linkTo(methodOn(MyController.class).method())
inside a @RepositoryRestController would produce a URL without the configured
basePath (e.g. /items instead of /api/items), even though incoming requests
were correctly routed via BasePathAwareHandlerMapping.

The factory is registered as a Spring bean in RepositoryRestMvcConfiguration
so it is available for injection and use throughout the application context.

Signed-off-by: ruthes00 <ruthes00@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Links to RepositoryRestController methods ignore basePath

2 participants