Skip to content

Links to RepositoryRestController methods ignore basePath #2509

Open

Description

@dschulten

I use RepositoryRestController to add addititional resource methods to Spring Data Rest Entities. When I try to add links for them to my URI space using linkTo(methodOn(Controller.class).someMethod(), Spring Data Rest generates a URL without the configured basePath.

Possibly related: #1782

Workaround: use EntityLinks.linkFor(Entity.class).slash("foo).slash("bar").withRel("my-foo"), but that is of course not refactoring-safe.

Activity

  1. ruthst00 commented on Sep 18, 2026

    @ruthst00

    This issue still exists in SDR 5.0.7 / Current Codebase

    How the routing side works (correctly)

    BasePathAwareHandlerMapping.getMappingForMethod() does prepend the basePath to the registered request mapping patterns at handler-mapping time. So incoming HTTP requests to /api/myentities/custom are correctly routed to the @RepositoryRestController method. This part works.

    // BasePathAwareHandlerMapping.java
    private String[] getBasePathedPrefixes(Class<?> handlerType) {
        ...
        return customPrefixes.length == 0
            ? new String[] { baseUri }  // prepends basePath
            : Arrays.stream(customPrefixes).map(baseUri::concat).toArray(String[]::new);
    }

    How the link-generation side works (which is broken)

    WebMvcLinkBuilder.linkTo(methodOn(...)) from Spring HATEOAS works by introspecting the @RequestMapping annotation on the controller method and building a URI from it. It does not consult the SDR BasePathAwareHandlerMapping or the SDR basePath configuration. It only sees the raw @RequestMapping("/myentities/custom") value on the method — without the base path prefix.

    There is no mechanism in the SDR codebase that registers the base-path-augmented mappings with Spring HATEOAS's WebMvcLinkBuilder infrastructure. The RepositoryRestMvcConfiguration sets up a DelegatingHandlerMapping with the BasePathAwareHandlerMapping, but Spring HATEOAS's WebMvcLinkBuilder uses its own MvcUriComponentsBuilder which reads annotations directly from the controller class, bypassing SDR's handler mapping entirely.

    No fix was introduced in 5.0.7

    The git log for the 5.0.7 release (d89ecee2) shows only a version bump commit — no changes to BasePathAwareHandlerMapping, RepositoryRestController, RepositoryRestMvcConfiguration, or any link-building infrastructure. There is no commit in the history that addresses issue #2509.


    Root Cause Summary

    Aspect Status
    Routing of requests to @RepositoryRestController (includes basePath) ✅ Works
    WebMvcLinkBuilder.linkTo(methodOn(...)) including basePath ❌ Broken
    Fix present in 5.0.7 ❌ No
    Fix present in current main (5.2.0-SNAPSHOT) ❌ No

    Recommended Workaround (still valid)

    Use RepositoryEntityLinks (or EntityLinks) to build links, which correctly uses the BaseUri:

    entityLinks.linkFor(MyEntity.class).slash("custom").withRel("my-rel")

    Or manually prepend the base path:

    @Autowired BaseUri baseUri;
    
    Link link = Link.of(baseUri.getUriComponentsBuilder()
        .path("/myentities/custom")
        .build().toUriString(), "my-rel");

    cc @odrotbohm

  2. added a commit that references this issue on Sep 18, 2026
    33753ad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions