Skip to content

UriComponentsBuilder controller method argument has no path #25822

@EtienneMiret

Description

@EtienneMiret

Affects: Spring WebFlux 5.2.9

From the documentation about Spring WebFlux controller annotated method arguments:

UriComponentsBuilder | For preparing a URL relative to the current request’s host, port, scheme, and path.

When reading this, I understood that the injected builder would include the full path to my controller method, which seemed confirmed by #20546 stating that an injected builder should behave like UriComponentsBuilder.fromHttpRequest(request).

However, when building a war that is deployed to tomcat 9.0.27 under the application context "/context":

@Controller
@RequestMapping ("/controller")
public class MyController {

  private static final Logger logger = LoggerFactory.getLogger (MyController.class);

  @GetMapping ("/method")
  public void method (
      UriComponentsBuilder builder,
      ServerHttpRequest request
  ) {
   logger.info ("Injected: {}", builder.build (emptyMap ())); // -> http://localhost:8080/
   logger.info ("Constructed: {}", UriComponentsBuilder.fromHttpRequest (request).build (emptyMap ())); // -> http://localhost:8080/context/controller/method
  }
}

Note that I’m fine if the spec changed after #20546 was drafted. The documentation however should be updated.

Interestingly, answers to #21449 state that the injected builder should be "application relative". My understanding of this sentence is that it should include the application context path, but not the controller path. This would have been exactly what I need, but isn’t what I see.

I haven’t tested what happens with non-reactive controllers. Obviously, we want the behavior to match in all three places:

  • WebFlux
  • Web MVC
  • latest documentation.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions