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

Regression in @RequestMapping parsing path variable in the url [SPR-6532] #11198

Closed
spring-projects-issues opened this issue Dec 7, 2009 · 4 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@spring-projects-issues
Copy link
Collaborator

Youen Chéné opened SPR-6532 and commented

Hi i've got the this spring mvc code which works fines with RC2.

@RequestMapping(value={"/product-{id}.html","/product-{id}-{name}.html"})
public String showProduct(@PathVariable String id,@RequestParam(value="loc",required=false) String location,Model model)

When I am switching with RC3, it is only handle the /product-{id}.html url pattern and not the /product-{id}-{name}.html pattern. In fact with this last pattern it try to put {id}-{name} in the id path variable instead of just put {id}.

Sorry, if it is a duplicate, after a quick looking it dot not seems to be one.


Affects: 3.0 RC3

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

What kind of HTTP requests are to be handled by this controller? I'm guessing something like /product-foo and /product-foo-bar, but I'd like to be sure. Are there .'s (periods) in the request?

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Dec 7, 2009

Arjen Poutsma commented

In RC3, we changed the way patterns are compared to figure out the best match. In RC2 and before, it used to be based on pattern length, but that resulted in strange behavior (see #10947).

From RC3, it's based on the amount of variables and wildcards used: the pattern with the least amount of wildcards wins. For a request like /product-1-2.html, that is /product-{id}.html. You can even see that in the log:

DefaultAnnotationHandlerMapping - Matching patterns for request [/product-5-6.html] are [/product-{id}.html, /product-{id}-{name}.html]

this logging statement shows all matches, in best matching order.

The way to fix your issue is to make sure that the {id} variable does not match dashes (-'s). You can do that by changing the regular expression for the mapping, like this:

@RequestMapping(value={"/product-{id:[^-]+}.html","/product-{id}-{name}.html"})
public String showProduct(@PathVariable String id,@RequestParam(value="loc",required=false) String location,Model model)

Then your code works fine (or at least in my tests).

On a related note: why are you using that {name} variable when you don't use it? I think /product-{id}-*.html would work for you as well.

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Reopening because of faulty resolution.

@spring-projects-issues
Copy link
Collaborator Author

Youen Chéné commented

Hi,

Thanks for the quick "how to". This explanation should be in the docs as example of handling path elements.

I'll test it tomorrow.

Thanks

PS: I've a big week and I don't understand why I missed the email notification?

@spring-projects-issues spring-projects-issues added type: bug A general bug status: declined A suggestion or change that we don't feel we should currently apply in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug A general bug label Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

2 participants