Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-projects-issues opened this issue Feb 5, 2014 · 2 comments
Assignees
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Feb 5, 2014

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

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 6, 2014

Amr Mostafa commented

Please note that in the first code snippet, changing build().getPath() to buildAndExpand(userId).getPath() fixes the issue. Therefore I'm no longer sure whether this is a bug or is just incorrect use of the API.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 6, 2014

Rossen Stoyanchev commented

This has been fixed in master.

changing build().getPath() to buildAndExpand(userId).getPath() fixes the issue

Actually it doesn't since you have to provide a userId method argument (even if null) and then expand it. That duplication should not be necessary. In this example the supplied method argument values should be sufficient to fill in the URI variable values. If the method did not actually have a method argument for a URI variable, then you would need to call .buildAndExpand() in addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants