spring-projects / spring-projects/spring-hateoas

WebMvcLinkBuilder generates links with doubled servlet mapping when alwaysUseFullPath=true

Open
#1,377 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

I've got pretty much the same problem as in https://github.com/spring-projects/spring-hateoas/issues/102 but with newer builder i.e. WebMvcLinkBuilder.

In short:

  • the rest servlet is mapped onto /rest/v1/* like this:

      @Configuration
      @EnableWebMvc
      @ImportAutoConfiguration(ServletWebServerFactoryAutoConfiguration.class)
      public class ServletConfig implements WebMvcConfigurer {
      
          @Bean
          public ServletContextInitializer servletContextInitializer() {
              return servletContext -> {
                     AnnotationConfigWebApplicationContext restCtx = new AnnotationConfigWebApplicationContext();
                     restCtx.register(RestConfig.class);
                     ServletRegistration.Dynamic restServletReg = servletContext.addServlet("rest", new DispatcherServlet(restCtx));
                     restServletReg.setLoadOnStartup(1);
                     restServletReg.addMapping("/rest/v1/*");
              };
          }
      
      }
    
  • alwaysUseFullPath is on in RestConfig

  • documents controller is mapped onto /rest/v1/documents

  • links are generated this way:

     public Document addDocument(@RequestBody Document document) {
         documents.add(document);
         final Link link = linkTo(methodOn(DocumentController.class).getDocuments())
                                   .withSelfRel()
                                   .withMedia(MediaType.APPLICATION_JSON_VALUE)
                                   .withTitle("Documents");
         document.add(link);
         return document;
     }
    
    

Then for a post to /rest/v1/documents I receive a link looking like this: http://localhost:8080/rest/v1/rest/v1/documents (note doubled servlet mapping)

The full example: https://github.com/artszko/spring-hateoas-links-issue, there is an integration test for the issue.

Did I misconfigure something or this is actually a bug?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the integration test in the linked full example and reproduce the duplicated /rest/v1 mapping with WebMvcLinkBuilder and alwaysUseFullPath=true. Trace how the builder combines the servlet mapping and controller path; done when the generated link contains the servlet mapping only once and the integration test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.