Skip to content

MvcUriComponentsBuilder does not expand path variables in controller-level @RequestMapping [SPR-11391] #16018

@spring-projects-issues

Description

@spring-projects-issues

Amr Mostafa opened SPR-11391 and commented

@RequestMapping("/user/{userId}/contacts")
class UserContactController {
    @RequestMapping("/create")
    @ResponseBody
    public String showCreate(@PathVariable Integer userId) {
        return MvcUriComponentsBuilder.fromMethodName(UserContactController.class, "showCreate", userId).build().getPath();
    }
}

Assuming this is running in a servlet mapped at "/", then requesting: http://localhost/user/123/contacts/create is expected to output:

/user/123/contacts/create

But the actual output is:

/user/{userId}/contacts/create

If we change the code to the following:

class UserContactController {
    @RequestMapping("/user/{userId}/contacts/create")
    @ResponseBody
    public String showCreate(@PathVariable Integer userId) {
        return MvcUriComponentsBuilder.fromMethodName(UserContactController.class, "showCreate", userId).build().getPath();
    }
}

Then the expected result is displayed.


Affects: 4.0 GA, 4.0.1

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